update send email verify ttl 300 sec

This commit is contained in:
Tokumeikoi 2020-03-13 14:32:36 +08:00
parent 01da63f82e
commit 13dbb143f8
3 changed files with 21 additions and 10 deletions

View File

@ -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
]);

View File

@ -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
]);

View File

@ -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;
}
}