Browse Source

用户忘记密码验证阿门过期时间调整

kely 8 months ago
parent
commit
556c21b004

+ 3 - 4
app/Http/Controllers/API/AuthController.php

@@ -74,7 +74,7 @@ class AuthController extends Controller
             // 如果记录存在,更新它
             DB::table('password_reset_tokens')->where('email', $user->email)->update([
                 'token' => $code,
-                'created_at' => Carbon::now(),
+                'exp_date' => Carbon::now(),
             ]);
         } else {
             // 如果记录不存在,插入新记录
@@ -82,6 +82,7 @@ class AuthController extends Controller
                 'email' => $user->email,
                 'token' => $code,
                 'created_at' => Carbon::now(),
+                'exp_date' => Carbon::now(),
             ]);
         }
         //3.发送重置验证码邮件
@@ -108,14 +109,12 @@ class AuthController extends Controller
             ->first(); // 获取第一条记录;
 
         //3.判断验证码是否存在 验证码是否一致 验证码是否过期(15分钟) 若过期,执行以下if代码
-        if (!$resetToken || $resetToken->token != $request->code || Carbon::parse($resetToken->created_at)->diffInMinutes(Carbon::now()) > 15){
+        if (!$resetToken || $resetToken->token != $request->code || Carbon::parse($resetToken->exp_date)->diffInMinutes(Carbon::now()) > 15){
             return $this->badRequest('Verification code error or expired');
         }
         //4.一切没问题,则修改该用户的密码
         $user->password = Hash::make($request->new_password);
         $user->save();
-        //删除验证码
-        DB::table('password_reset_tokens')->where('email', $resetToken->email)->delete();
         return $this->noContent();
 
     }

+ 30 - 0
database/migrations/2024_07_05_141543_add_exp_date_to_password_reset_tokens.php

@@ -0,0 +1,30 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::table('password_reset_tokens', function (Blueprint $table) {
+            //
+            $table->date("exp_date");
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::table('password_reset_tokens', function (Blueprint $table) {
+            //
+            $table->dropColumn(['exp_date']);
+        });
+    }
+};

+ 6 - 6
resources/views/emails/user/forgetPasswordCaptcha.blade.php

@@ -1,12 +1,12 @@
 <x-mail::message>
-    ## You are resetting your password, please ignore it if you are not doing it yourself.
+## You are resetting your password, please ignore it if you are not doing it yourself.
 
-    # CODE
+# CODE
 
-    # {{$code}}
+# {{$code}}
 
-    ### Valid in 15 minutes
+### Valid in 15 minutes
 
-    [Go to reset password]({{config('app.url').'/reset-password'}})<br>
-    {{ config('app.name') }}
+[Go to reset password]({{config('app.url').'/reset-password'}})<br>
+From  {{ config('app.name') }}
 </x-mail::message>