The Problem
Users forget their passwords. You need a secure way to let them reset it without compromising account security.
Solution: Laravel's Built-in Password Reset
Step 1: Configure Mail Settings
# .env
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_FROM_ADDRESS=noreply@yourapp.com
MAIL_FROM_NAME="${APP_NAME}"
Step 2: Create Password Reset Routes
// routes/web.php
Route::get('/forgot-password', [PasswordResetController::class, 'showForm'])
->name('password.request');
Route::post('/forgot-password', [PasswordResetController::class, 'sendLink'])
->middleware('throttle:5,1')
->name('password.email');
Route::get('/reset-password/{token}', [PasswordResetController::class, 'showResetForm'])
->name('password.reset');
Route::post('/reset-password', [PasswordResetController::class, 'reset'])
->name('password.update');
Step 3: Create the Controller
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class PasswordResetController extends Controller
{
public function sendLink(Request $request)
{
$request->validate(['email' => 'required|email']);
$status = Password::sendResetLink(
$request->only('email')
);
return $status === Password::RESET_LINK_SENT
? back()->with('status', 'Password reset link sent!')
: back()->withErrors(['email' => __($status)]);
}
public function reset(Request $request)
{
$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) {
$user->forceFill([
'password' => Hash::make($password)
])->setRememberToken(Str::random(60));
$user->save();
}
);
return $status === Password::PASSWORD_RESET
? redirect()->route('login')->with('status', __($status))
: back()->withErrors(['email' => [__($status)]]);
}
}
Step 4: Customize the Email Template
php artisan vendor:publish --tag=laravel-notifications
Security Best Practices
- Rate limit password reset requests
- Set token expiration (default: 60 minutes)
- Log password reset attempts
- Invalidate all sessions after reset
That's it! Your users can now securely reset their passwords.
Comments (0)
Leave a Comment
No comments yet. Be the first to share your thoughts!