From 13dbb143f8382da9011231cb44afbbe9e0a8434e Mon Sep 17 00:00:00 2001 From: Tokumeikoi Date: Fri, 13 Mar 2020 14:32:36 +0800 Subject: [PATCH] update send email verify ttl 300 sec --- app/Http/Controllers/Passport/AuthController.php | 11 +++++------ app/Http/Controllers/Passport/CommController.php | 9 +++++---- app/Utils/CacheKey.php | 11 +++++++++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Passport/AuthController.php b/app/Http/Controllers/Passport/AuthController.php index b501998e..e4cf280a 100644 --- a/app/Http/Controllers/Passport/AuthController.php +++ b/app/Http/Controllers/Passport/AuthController.php @@ -13,6 +13,7 @@ use App\Models\User; use App\Models\InviteCode; use App\Utils\Helper; use App\Utils\Dict; +use App\Utils\CacheKey; class AuthController extends Controller { @@ -35,11 +36,10 @@ class AuthController extends Controller } } if ((int)config('v2board.email_verify', 0)) { - $redisKey = 'sendEmailVerify:' . $request->input('email'); if (empty($request->input('email_code'))) { abort(500, '邮箱验证码不能为空'); } - if (Cache::get($redisKey) !== $request->input('email_code')) { + if (Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))) !== $request->input('email_code')) { abort(500, '邮箱验证码有误'); } } @@ -86,7 +86,7 @@ class AuthController extends Controller abort(500, '注册失败'); } if ((int)config('v2board.email_verify', 0)) { - Cache::forget($redisKey); + Cache::forget(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))); } $request->session()->put('email', $user->email); $request->session()->put('id', $user->id); @@ -189,8 +189,7 @@ class AuthController extends Controller public function forget(AuthForget $request) { - $redisKey = 'sendEmailVerify:' . $request->input('email'); - if (Cache::get($redisKey) !== $request->input('email_code')) { + if (Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))) !== $request->input('email_code')) { abort(500, '邮箱验证码有误'); } $user = User::where('email', $request->input('email'))->first(); @@ -202,7 +201,7 @@ class AuthController extends Controller if (!$user->save()) { abort(500, '重置失败'); } - Cache::forget($redisKey); + Cache::forget(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))); return response([ 'data' => true ]); diff --git a/app/Http/Controllers/Passport/CommController.php b/app/Http/Controllers/Passport/CommController.php index bae6e8da..8d473b31 100644 --- a/app/Http/Controllers/Passport/CommController.php +++ b/app/Http/Controllers/Passport/CommController.php @@ -12,6 +12,7 @@ use Illuminate\Support\Facades\Cache; use App\Jobs\SendEmail; use App\Models\InviteCode; use App\Utils\Dict; +use App\Utils\CacheKey; class CommController extends Controller { @@ -38,11 +39,10 @@ class CommController extends Controller public function sendEmailVerify(CommSendEmailVerify $request) { $email = $request->input('email'); - $cacheKey = 'sendEmailVerify:' . $email; - if (Cache::get($cacheKey)) { + if (Cache::get(CacheKey::get('LAST_SEND_EMAIL_VERIFY_TIMESTAMP', $email))) { abort(500, '验证码已发送,请过一会再请求'); } - $code = Helper::randomChar(6); + $code = rand(100000, 999999); $subject = config('v2board.app_name', 'V2Board') . '邮箱验证码'; SendEmail::dispatch([ @@ -56,7 +56,8 @@ class CommController extends Controller ] ])->onQueue('verify_mail'); - Cache::put($cacheKey, $code, 60); + Cache::put(CacheKey::get('EMAIL_VERIFY_CODE', $email), $code, 300); + Cache::put(CacheKey::get('LAST_SEND_EMAIL_VERIFY_TIMESTAMP', $email), time(), 60); return response([ 'data' => true ]); diff --git a/app/Utils/CacheKey.php b/app/Utils/CacheKey.php index a1e466b4..302d43f4 100644 --- a/app/Utils/CacheKey.php +++ b/app/Utils/CacheKey.php @@ -4,5 +4,16 @@ namespace App\Utils; class CacheKey { + CONST KEYS = [ + 'EMAIL_VERIFY_CODE' => '邮箱验证吗', + 'LAST_SEND_EMAIL_VERIFY_TIMESTAMP' => '最后一次发送邮箱验证码时间' + ]; + public static function get(string $key, $uniqueValue) + { + if (!in_array($key, array_keys(self::KEYS))) { + abort(500, 'key is not in cache key list'); + } + return $key . '_' . $uniqueValue; + } }