In this tutorial I will show you how to create password reset token manually in Laravel Framework.
In some cases you may need to customize the password reset functionality in your Laravel Application. For example if you want to customize the email notifications and you do not want to use Laravel’s Password::sendResetLink
or you cannot use the User::
sendPasswordResetNotification
method to overwrite the reset password functionality etc. in a Laravel Application.
So if you want to create password reset token manually in Laravel you just need to follow these steps given below.
Steps to create password reset token
Include Password
facade wherever you want to create password reset token.
use Illuminate\Support\Facades\Password;
Create token
$user = User::find( 123 ); $token = Password::broker()->createToken($user); dump($token);
Output:
"26c618c117861aa29fda44f7462412e868bb96bea873c5c31b18fa9f8ab3aeaf"
How does it work?
When you call Password::broker()->createToken($user)
it creates a hashed value and saves it into your Laravel Applications password_resets
table. However it returns a raw token value (as above) which you can use to send to the user for completing the reset action later.
Later you can use this $token to reset your password. An example comes from core password reset methods provided by Laravel:
public function reset(Request $request) { $request->validate([ 'token' => 'required', 'email' => 'required|email', 'password' => 'required|confirmed', ]); $status = Password::reset( $request->only('email', 'password', 'password_confirmation', 'token'), function ($user) use ($request) { $user->forceFill([ 'password' => $request->password, 'remember_token' => Str::random(60), ])->save(); $user->tokens()->delete(); event(new PasswordReset($user)); } ); if ($status == Password::PASSWORD_RESET) { return response([ 'message'=> 'Password reset successfully' ]); } return response([ 'message'=> __($status) ], 500); }
I hope this short example can help you with creating password reset token manually in your Laravel Appliction.