Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

semour / semour_web

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • semour_web
  • app
  • Http
  • Controllers
  • Api
  • AuthApiController.php
AuthApiController.php 8.03 KB
杨树贤's avatar
api返回
186d5283
 
杨树贤 committed 2 years ago
1 2 3 4
<?php

namespace App\Http\Controllers\Api;

杨树贤's avatar
独立出请求验证
0c1b982d
 
杨树贤 committed 2 years ago
5
use App\Http\Requests\UserRegister;
杨树贤's avatar
用户对接自动分发
0d8576fa
 
杨树贤 committed 2 years ago
6
use App\Http\Services\UserService;
杨树贤's avatar
对接正式的邮箱
430e09f8
 
杨树贤 committed 2 years ago
7
use App\Mail\SendCode;
杨树贤's avatar
用户模型修改
ab5dd96d
 
杨树贤 committed 2 years ago
8
use App\Models\UserModel;
杨树贤's avatar
注册接口
f3006e7d
 
杨树贤 committed 2 years ago
9
use Illuminate\Foundation\Auth\RegistersUsers;
杨树贤's avatar
api返回
186d5283
 
杨树贤 committed 2 years ago
10 11
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Http\Request;
杨树贤's avatar
cookie信息
64fa34fc
 
杨树贤 committed 2 years ago
12
use Illuminate\Http\Response;
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
13
use Illuminate\Support\Facades\Auth;
杨树贤's avatar
cookie信息
64fa34fc
 
杨树贤 committed 2 years ago
14
use Illuminate\Support\Facades\Cookie;
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
15 16 17
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redis;
杨树贤's avatar
登陆接口
3638edfd
 
杨树贤 committed 2 years ago
18
use Illuminate\Support\Facades\Validator;
杨树贤's avatar
api返回
186d5283
 
杨树贤 committed 2 years ago
19 20 21 22

class AuthApiController extends Controller
{

杨树贤's avatar
登陆态问题
f7488c94
 
杨树贤 committed 2 years ago
23
    use RegistersUsers;
杨树贤's avatar
注册接口
f3006e7d
 
杨树贤 committed 2 years ago
24

杨树贤's avatar
独立出请求验证
0c1b982d
 
杨树贤 committed 2 years ago
25
    public function register(UserRegister $request)
杨树贤's avatar
注册接口
f3006e7d
 
杨树贤 committed 2 years ago
26
    {
杨树贤's avatar
询价接口
c63800c0
 
杨树贤 committed 2 years ago
27
        $email = $request->input('email');
杨树贤's avatar
用户模型修改
ab5dd96d
 
杨树贤 committed 2 years ago
28
        if (UserModel::where('email', $email)->exists()) {
杨树贤's avatar
询价接口
c63800c0
 
杨树贤 committed 2 years ago
29 30 31
            return $this->setError('Email has been taken');
        }

杨树贤's avatar
优化接口
045dfe30
 
杨树贤 committed 2 years ago
32
        //判断邮箱验证码
杨树贤's avatar
询价接口
c63800c0
 
杨树贤 committed 2 years ago
33
        $redisKey = 'sem_email_code_register_' . $email;
杨树贤's avatar
优化接口
045dfe30
 
杨树贤 committed 2 years ago
34 35 36 37
        $cachedEmailCode = Redis::get($redisKey);
        if ($cachedEmailCode != $request->input('email_code')) {
            return $this->setError('Email code invalid');
        }
杨树贤's avatar
用户对接自动分发
0d8576fa
 
杨树贤 committed 2 years ago
38
        $userId = UserService::createUser($request->all());
杨树贤's avatar
新增忘记密码流程接口
d664f163
 
杨树贤 committed 2 years ago
39
        Redis::del($redisKey);
杨树贤's avatar
注册新增字段
4e77187f
 
杨树贤 committed 2 years ago
40
        \Auth::loginUsingId($userId);
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
41
        return $this->setSuccess('Register success');
杨树贤's avatar
注册接口
f3006e7d
 
杨树贤 committed 2 years ago
42 43
    }

杨树贤's avatar
api返回
186d5283
 
杨树贤 committed 2 years ago
44 45 46

    public function login(Request $request)
    {
杨树贤's avatar
登陆接口
3638edfd
 
杨树贤 committed 2 years ago
47 48
        $validator = Validator::make($request->all(), [
            'email' => 'required|string|email|max:255',
杨树贤's avatar
修改校验规则
7242a91d
 
杨树贤 committed 2 years ago
49
            'password' => 'required|string|min:8|max:50',
杨树贤's avatar
登陆接口
3638edfd
 
杨树贤 committed 2 years ago
50 51 52 53 54 55
        ], [
            'password.min' => 'Password must be at least 8 characters long.'
        ]);

        if ($validator->fails()) {
            return $this->setError($validator->errors()->first());
杨树贤's avatar
api返回
186d5283
 
杨树贤 committed 2 years ago
56 57
        }

杨树贤's avatar
用户模型修改
ab5dd96d
 
杨树贤 committed 2 years ago
58
        $userExists = UserModel::where('email', $request->email)->exists();
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
59 60 61 62
        if (!$userExists) {
            return $this->setError('User dose not exist');
        }

杨树贤's avatar
api返回
186d5283
 
杨树贤 committed 2 years ago
63
        if ($this->attemptLogin($request)) {
杨树贤's avatar
登陆接口
3638edfd
 
杨树贤 committed 2 years ago
64
            $request->session()->regenerate();
杨树贤's avatar
cookie安全性
16b910ae
 
杨树贤 committed 2 years ago
65
            $cookie = Cookie::make('sem_email', Auth::user()->email, config('session.lifetime'));
杨树贤's avatar
https的cookie
cd3850d8
 
杨树贤 committed 2 years ago
66
            Cookie::queue($cookie);
杨树贤's avatar
寻报价数量
ff2c8438
 
杨树贤 committed 2 years ago
67 68 69
            $userIdCookie = Cookie::make('sem_user_id', Auth::user()->id, config('session.lifetime'), null, null, false,
                false);
            Cookie::queue($userIdCookie);
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
70
            return $this->setSuccess('Login success');
杨树贤's avatar
api返回
186d5283
 
杨树贤 committed 2 years ago
71 72 73
        }


杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
74 75 76 77 78
        return $this->setError('Login failure');
    }

    public function logout(Request $request)
    {
杨树贤's avatar
登陆问题
90ad5746
 
杨树贤 committed 2 years ago
79 80
        $cookie = Cookie::forget('sem_email');
        Cookie::queue($cookie);
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
81 82 83 84 85 86
        Auth::logout();
        return $this->setSuccess('Logout success');
    }

    public function resetPassword(Request $request)
    {
杨树贤's avatar
邮箱
0dc9b2db
 
杨树贤 committed 2 years ago
87

杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
88
        $validator = Validator::make($request->all(), [
杨树贤's avatar
修改校验规则
7242a91d
 
杨树贤 committed 2 years ago
89 90
            'old_password' => 'required|min:8|max:50',
            'password' => ['required', 'string', 'min:8', 'max:50', 'confirmed'],
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
91 92 93 94 95 96 97 98 99 100
        ], [
            'old_password.min' => 'Password must be at least 8 characters long.',
            'password.min' => 'Password must be at least 8 characters long.',
            'password.confirmed' => 'Passwords do not match!',
        ]);

        if ($validator->fails()) {
            return $this->setError($validator->errors()->first());
        }
        $password = $request->get('password');
杨树贤's avatar
邮箱
0dc9b2db
 
杨树贤 committed 2 years ago
101 102
        $oldPassword = $request->get('old_password');
        $userId = Auth::user()->id;
杨树贤's avatar
用户模型修改
ab5dd96d
 
杨树贤 committed 2 years ago
103
        $hashedPassword = UserModel::where('id', $userId)->value('password');
杨树贤's avatar
邮箱
0dc9b2db
 
杨树贤 committed 2 years ago
104
        if (!Hash::check($oldPassword, $hashedPassword)) {
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
105 106 107
            return $this->setError('Wrong Password!');
        }

杨树贤's avatar
页码
aaca8261
 
杨树贤 committed 2 years ago
108 109 110 111
        if (Hash::check($password, $hashedPassword)) {
            return $this->setError('The new password cannot be the same as the old password!');
        }

杨树贤's avatar
用户模型修改
ab5dd96d
 
杨树贤 committed 2 years ago
112
        $user = UserModel::find($userId);
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
113 114 115 116 117 118 119 120 121 122
        $user->password = Hash::make($password);
        $user->update_time = time();
        $result = $user->save();
        if (!$result) {
            return $this->setError('Reset password failed');
        }

        return $this->setSuccess('Reset password success');
    }

杨树贤's avatar
新增忘记密码流程接口
d664f163
 
杨树贤 committed 2 years ago
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    public function forgetPassword(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'email_code' => 'required'
        ], []);
        if ($validator->fails()) {
            return $this->setError($validator->errors()->first());
        }
        $email = $request->input('email');
        $emailCode = $request->input('email_code');
        $redisKey = 'sem_email_code_forget_password' . '_' . $email;
        $cachedEmailCode = Redis::get($redisKey);
        if ($cachedEmailCode != $emailCode) {
            return $this->setError('Email code invalid');
        }
        return $this->setSuccess('Verify email code success');
    }

    //忘记密码以后的设置密码
    public function setForgetPassword(Request $request)
    {
        $validator = Validator::make($request->all(), [
杨树贤's avatar
修改校验规则
7242a91d
 
杨树贤 committed 2 years ago
146
            'password' => ['required', 'string', 'min:8', 'max:50', 'confirmed'],
杨树贤's avatar
新增忘记密码流程接口
d664f163
 
杨树贤 committed 2 years ago
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
            'email_code' => 'required',
            'email' => 'required|email'
        ], []);
        $password = $request->input('password');
        $emailCode = $request->input('email_code');
        $email = $request->input('email');
        if ($validator->fails()) {
            return $this->setError($validator->errors()->first());
        }
        $redisKey = 'sem_email_code_forget_password' . '_' . $email;
        $cachedEmailCode = Redis::get($redisKey);
        if (!$cachedEmailCode) {
            return $this->setError('Email code expired');
        }
        if ($cachedEmailCode != $emailCode) {
            return $this->setError('Email code invalid');
        }

mushishixian's avatar
重置密码优化
180d0e8f
 
mushishixian committed 2 years ago
165 166 167 168
        $hashedPassword = UserModel::where('email', $email)->value('password');
        if (Hash::check($password, $hashedPassword)) {
            return $this->setError('The new password cannot be the same as the old password!');
        }
杨树贤's avatar
新增忘记密码流程接口
d664f163
 
杨树贤 committed 2 years ago
169 170 171 172 173 174 175 176 177 178 179 180 181

        $user = UserModel::where('email', $email)->first();
        $user->password = Hash::make($password);
        $user->update_time = time();
        $result = $user->save();
        if (!$result) {
            return $this->setError('Reset password failed');
        }
        Redis::del($redisKey);
        Auth::logout();
        return $this->setSuccess('Reset password success');
    }

杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
182 183 184 185 186
    //发送邮箱验证码
    public function sendEmailCode(Request $request)
    {
        $email = $request->input('email');
        $type = $request->input('type', 'register');
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
187 188 189 190 191 192

        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
        ]);
        if ($validator->fails()) {
            return $this->setError($validator->errors()->first());
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
193
        }
杨树贤's avatar
用户模型修改
ab5dd96d
 
杨树贤 committed 2 years ago
194
        $info = UserModel::where('email', $email)->first();
杨树贤's avatar
新增忘记密码流程接口
d664f163
 
杨树贤 committed 2 years ago
195 196 197 198 199
        switch ($type) {
            case 'register':
                if ($info) {
                    return $this->setError('This email had been registered');
                }
杨树贤's avatar
邮箱和询价问题
660d3237
 
杨树贤 committed 2 years ago
200
                break;
杨树贤's avatar
新增忘记密码流程接口
d664f163
 
杨树贤 committed 2 years ago
201
            case 'forget_password' :
杨树贤's avatar
邮箱和询价问题
660d3237
 
杨树贤 committed 2 years ago
202
            case 'update_email':
杨树贤's avatar
新增忘记密码流程接口
d664f163
 
杨树贤 committed 2 years ago
203 204 205 206
                $existEmail = UserModel::where('email', $email)->exists();
                if (!$existEmail) {
                    return $this->setError('This email is not registered');
                }
杨树贤's avatar
对接正式的邮箱
430e09f8
 
杨树贤 committed 2 years ago
207
                break;
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
208 209
        }

杨树贤's avatar
新增忘记密码流程接口
d664f163
 
杨树贤 committed 2 years ago
210

杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
211 212
        //发送验证码
        $code = mt_rand(1000, 9999);
杨树贤's avatar
邮箱
0dc9b2db
 
杨树贤 committed 2 years ago
213
        $redisKey = 'sem_email_code_' . $type . '_' . $email;
杨树贤's avatar
询价接口
c63800c0
 
杨树贤 committed 2 years ago
214
        if (Redis::get($redisKey)) {
杨树贤's avatar
修复展示
36c78999
 
杨树贤 committed 2 years ago
215
            return $this->setError('A verification code has been sent. Please enter the code below to continue.' . (config('app.debug') ? $code : ''));
杨树贤's avatar
询价接口
c63800c0
 
杨树贤 committed 2 years ago
216
        }
杨树贤's avatar
邮箱
0dc9b2db
 
杨树贤 committed 2 years ago
217
        Redis::set($redisKey, $code);
杨树贤's avatar
新增忘记密码流程接口
d664f163
 
杨树贤 committed 2 years ago
218
        Redis::expire($redisKey, 120);
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
219
        $subject = config('mail.from.name');
杨树贤's avatar
下单页面添加登陆判断
d87ac9f0
 
杨树贤 committed 2 years ago
220

mushishixian's avatar
邮箱去除
7d67b2c3
 
mushishixian committed 2 years ago
221
        // return $this->setSuccessData($code);
杨树贤's avatar
邮箱恢复
7497bd3d
 
杨树贤 committed 2 years ago
222 223 224 225 226
        Mail::to($email)->send(new SendCode($type, $code));
        //错误处理
        if (count(Mail::failures())) {
            return $this->setError('Email code send failed');
        }
杨树贤's avatar
重置秘密和邮箱相关接口
1a52f4a5
 
杨树贤 committed 2 years ago
227

杨树贤's avatar
修复展示
36c78999
 
杨树贤 committed 2 years ago
228
        return $this->setSuccess('A verification code has been sent. Please enter the code below to continue. ' . (config('app.debug') ? $code : ''));
杨树贤's avatar
api返回
186d5283
 
杨树贤 committed 2 years ago
229 230 231
    }

}