TAY
笔记 · · 阅读 177

laravel 忘记密码 重置密码

路由

Route::group(['middleware' => ['guest']], function () {
    Route::post('/forgot-password', 'ForgotPasswordController@forgot')->name('password.forgot');
    Route::post('/reset-password', 'ForgotPasswordController@reset')->name('password.reset');
});

User 模型

use App\Notifications\PasswordReset;
use Illuminate\Support\Facades\Mail;

public function sendPasswordResetNotification($token)
    {
        // $this->notify(new MyCustomResetPasswordNotification($token)); <--- remove this, use Mail instead like below
        $data = [
            $this->email
        ];

        Mail::send('email.reset_password', [
            'fullname'      => $this->fullname,
            'reset_url'     => config('app.frontend_url').'/auth/reset_password?token='.$token.'&email='.$this->email,
        ], function($message) use($data){
            $message->subject('密码重置');
            $message->to($data[0]);
        });
    }

控制器 ForgotPasswordController

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;

use App\Models\User;

class ForgotPasswordController extends Controller
{
    public function forgot(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email|exists:users,email',
        ], [
            'email.required' => '邮箱必填',
            'email.email' => '邮箱格式不正确',
            'email.exists' => '数据库中不存在这个邮箱地址',
        ]);
        $response = Password::sendResetLink($request->only('email'));
        return ['state'=>$response == 'passwords.sent' ? 'success' : 'error', 'msg'=>__($response)];
    }
    
    public function reset(Request $request, $user_id = '')
    {
        $request->validate([
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|min:8|confirmed',
        ]);
        $status = Password::reset(
            $request->only('email', 'password', 'password_confirmation', 'token'),
            function ($user, $password) use ($request) {
                $user->forceFill([
                    'password' => Hash::make($password)
                ])->save();

                $user->setRememberToken(Str::random(60));

                event(new PasswordReset($user));
            }
        );
        return $status == Password::PASSWORD_RESET ? ['state'=>'success', 'msg' => __($status)] : ['state'=>'error', 'msg' => __($status)];
    }
}

邮件模板

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<style>
.wrap{width:100%;background-color:#eee;padding: 5rem 0;}
.card{width:600px;background-color:#fff;margin:auto;padding:2rem;box-shadow: 0 7px 14px 0 rgba(151,160,204,.12) !important;border-radius: .375rem;}
.head{font-size:17px;font-weight:bold;margin-bottom:2rem;}
.foot{text-align:right;margin-top:2rem;}
</style>
<div class="wrap">
    <div class="card">
        <div class="head">尊敬的用户您好!</div>
        <div class="content">您正在申请重置密码,请点击链接以继续<a href="{{ $reset_url }}">{{ $reset_url }}</a>,链接有效期 10 分钟。如非本人操作忽略即可。</div>
        <div class="foot">{{ config('app.name') }} 管理团队</div>
    </div>
</div>
</body>
</html>
目录