要編寫自己的密碼重置邏輯,您仍然可以使用默認遷移,或者直接創建自己的密碼。最重要的部分是令牌。由於您正在自行設置密碼,因此您需要做出幾項決定:
您將需要2頁,4個不同的路線和4個不同的功能在同一個控制器。 '我忘記了我的密碼'頁面和'重置密碼'頁面。在第一頁中,顯示一個表單,您可以在其中接收用戶電子郵件。併發布到以下控制器。
//to be added on top as use statements
use DB;
use Auth;
use Hash;
use Carbon;
use App\User;
public function sendPasswordResetToken(Request $request)
{
$user = User::where ('email', $request->email)-first();
if (!$user) return redirect()->back()->withErrors(['error' => '404']);
//create a new token to be sent to the user.
DB::table('password_resets')->insert([
'email' => $request->email,
'token' => str_random(60), //change 60 to any length you want
'created_at' => Carbon::now()
]);
$tokenData = DB::table('password_resets')
->where('email', $request->email)->first();
$token = $tokenData->token;
$email = $request->email; // or $email = $tokenData->email;
/**
* Send email to the email above with a link to your password reset
* something like url('password-reset/' . $token)
* Sending email varies according to your Laravel version. Very easy to implement
*/
}
第二部分,當鏈路
/**
* Assuming the URL looks like this
* http://localhost/password-reset/random-string-here
* You check if the user and the token exist and display a page
*/
public function showPasswordResetForm($token)
{
$tokenData = DB::table('password_resets')
->where('token', $token)->first();
if (!$tokenData) return redirect()->to('home'); //redirect them anywhere you want if the token does not exist.
return view('passwords.show');
}
顯示含有2個輸入 形式在頁面上用戶點擊 - 新密碼password
或whateveer你想 - 新密碼確認password_confirm
或什麼您需要 表單應發佈到映射到以下控制器的相同URL。爲什麼?因爲我們仍然需要使用令牌來查找實際用戶。
public function resetPassword(Request $request, $token)
{
//some validation
...
$password = $request->password;
$tokenData = DB::table('password_resets')
->where('token', $token)->first();
$user = User::where('email', $tokenData->email)->first();
if (!$user) return redirect()->to('home'); //or wherever you want
$user->password = Hash::make($password);
$user->update(); //or $user->save();
//do we log the user directly or let them login and try their password for the first time ? if yes
Auth::login($user);
// If the user shouldn't reuse the token later, delete the token
DB::table('password_resets')->where('email', $user->email')->delete();
//redirect where we want according to whether they are logged in or not.
}
不要忘了添加路由
Route::get('password-reset', '[email protected]'); //I did not create this controller. it simply displays a view with a form to take the email
Route::post('password-reset', '[email protected]');
Route::get('reset-password/{token}', '[email protected]');
Route::post('reset-password/{token}', '[email protected]');
注意:可能有錯別字或語法錯誤,因爲我沒有測試這一點,並在這裏直接從我的頭頂寫的。如果你看到一個錯誤/異常,請不要驚慌失措,閱讀錯誤並搜索谷歌。
我在'config/app.php'中看不到'driver'。但是如果您仍然需要它,我可以幫助您進行自定義密碼重置。 – EddyTheDove
我的不好,它是'config/auth.php',而不是'config/app.php',指定了驅動程序設置。我會糾正這個帖子。非常感謝自定義密碼重置幫助。 –