How to Implement Password Reset in Laravel 5: Step‑by‑Step Guide
This tutorial explains how to set up Laravel 5's built‑in password reset feature, covering the required data tables, model contracts, route definitions, view templates, email configuration, and the complete reset workflow with code examples.
Forgot password is a common scenario; Laravel 5 provides built‑in support for password reset with minimal configuration.
1. Implementation Idea
Send a password‑reset link containing a token to the user's registered email; the user follows the link to set a new password.
2. Data Table & Model
The default User model implements CanResetPasswordContract via the CanResetPassword trait. Laravel also provides the password_resets table (migration created earlier) to store reset tokens.
3. Create Routes
<code>// Send password reset link routes
Route::get('password/email', 'Auth\PasswordController@getEmail');
Route::post('password/email', 'Auth\PasswordController@postEmail');
// Password reset routes
Route::get('password/reset/{token}', 'Auth\PasswordController@getReset');
Route::post('password/reset', 'Auth\PasswordController@postReset');
</code>4. Create Views
Define the view for sending the reset link ( resources/views/auth/password.blade.php ) and the view for resetting the password ( resources/views/auth/reset.blade.php ).
<code><form method="POST" action="/password/email">
{!! csrf_field() !!}
<div>Email<input type="email" name="email" value="{{ old('email') }}"></div>
<div><button type="submit">Send Password Reset Link</button></div>
</form>
</code> <code><form method="POST" action="/password/reset">
{!! csrf_field() !!}
<input type="hidden" name="token" value="{{ $token }}">
<div>Email: <input type="email" name="email" value="{{ old('email') }}"></div>
<div>New Password: <input type="password" name="password"></div>
<div>Confirm Password: <input type="password" name="password_confirmation"></div>
<div><button type="submit">Reset Password</button></div>
</form>
</code>Also create an email template view ( resources/views/emails/password.blade.php ) that contains the reset link:
<code>Click here to reset your password: {{ url('password/reset/'.$token) }}
</code>5. Email Configuration
Configure config/mail.php (driver, host, port, username, password, encryption) and set the from address to match the MAIL_USERNAME in the .env file.
<code>return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => null, 'name' => null],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
</code>Example .env settings for a 163.com mailbox:
<code>MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=25
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=null
</code>Set the from address in config/mail.php to the same email:
<code>'from' => ['address' => '[email protected]', 'name' => 'Laravel Academy'],
</code>6. Reset Password Process
Visit /password/email , enter the registered email, and submit. Laravel sends a reset email containing a token‑based link. Clicking the link opens the reset form; after entering the new password and confirming, the password is updated, the token record is removed, and the user is redirected (default /home , configurable via $redirectPath in PasswordController ).
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.