Commit d664f163 by 杨树贤

新增忘记密码流程接口

parent dc27d38c
......@@ -3,8 +3,11 @@
<component name="ChangeListManager">
<list default="true" id="fb90add0-1393-48c2-9f26-72365d42cd03" name="变更" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Controllers/Api/AuthApiController.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Controllers/Api/AuthApiController.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Controllers/Api/UserApiController.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Controllers/Api/UserApiController.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/bootstrap/app.php" beforeDir="false" afterPath="$PROJECT_DIR$/bootstrap/app.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/bootstrap/cache/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/bootstrap/cache/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/routes/api.php" beforeDir="false" afterPath="$PROJECT_DIR$/routes/api.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/storage/app/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/storage/app/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/storage/app/public/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/storage/app/public/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/storage/framework/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/storage/framework/.gitignore" afterDir="false" />
......@@ -186,7 +189,7 @@
<workItem from="1666835076791" duration="693000" />
<workItem from="1667266026118" duration="40321000" />
<workItem from="1667959054458" duration="183000" />
<workItem from="1667986756173" duration="7812000" />
<workItem from="1667986756173" duration="10295000" />
</task>
<servers />
</component>
......
......@@ -32,7 +32,7 @@ class AuthApiController extends Controller
return $this->setError('Email code invalid');
}
$userId = UserModel::createUser($request->all());
Redis::del($redisKey);
\Auth::loginUsingId($userId);
return $this->setSuccess('Register success');
}
......@@ -106,6 +106,60 @@ class AuthApiController extends Controller
return $this->setSuccess('Reset password success');
}
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(), [
'password' => ['required', 'string', 'min:8', 'confirmed'],
'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');
}
$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');
}
//发送邮箱验证码
public function sendEmailCode(Request $request)
{
......@@ -119,10 +173,19 @@ class AuthApiController extends Controller
return $this->setError($validator->errors()->first());
}
$info = UserModel::where('email', $email)->first();
if ($info && $type == 'register') {
return $this->setError('This email had been registered');
switch ($type) {
case 'register':
if ($info) {
return $this->setError('This email had been registered');
}
case 'forget_password' :
$existEmail = UserModel::where('email', $email)->exists();
if (!$existEmail) {
return $this->setError('This email is not registered');
}
}
//发送验证码
$code = mt_rand(1000, 9999);
$redisKey = 'sem_email_code_' . $type . '_' . $email;
......@@ -130,7 +193,7 @@ class AuthApiController extends Controller
return $this->setError('Email code had been sent');
}
Redis::set($redisKey, $code);
Redis::expire($redisKey, 60);
Redis::expire($redisKey, 120);
$subject = config('mail.from.name');
$msg = 'Email Code:' . $code . '.';
return $this->setSuccessData($code);
......
......@@ -105,7 +105,7 @@ class UserApiController extends Controller
if (!$result) {
return $this->setError('Change email failed');
}
Redis::del($redisKey);
return $this->setSuccess('Change email success');
}
......
......@@ -18,6 +18,8 @@ Route::middleware(['api'])->namespace('Api')->group(function () {
Route::POST('/auth/login', 'AuthApiController@login');
Route::POST('/auth/register', 'AuthApiController@register');
Route::POST('auth/send_email_code', 'AuthApiController@sendEmailCode');
Route::POST('auth/forget_password', 'AuthApiController@forgetPassword');
Route::POST('auth/set_forget_password', 'AuthApiController@setForgetPassword');
Route::get('country/list', 'CountryApiController@list');
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment