Laravelâs default authentication setup assumes youâre using a User model. But what if your app requires a different modelâlike Guest?
In this quick guide, Iâll show you how to make Laravel Breeze work with a custom model, and how to fix the common âForgot Passwordâ issue when using anything other than the default User model.
â What Iâm Using
**Laravel Breeze** for lightweight authentication scaffolding
A custom Guest model instead of the default User model
đĄ The Problem
Everything works greatâlogin, registration, etc.âuntil a guest tries to reset their password.
Laravelâs built-in "Forgot Password" functionality fails. Why?
Because by default, Laravelâs password reset is configured to use the users table and the User model. If you're using guests, this wonât work out of the box.
đ ď¸ The Fix
To get password resets working with your Guest model, head over to config/auth.php and update the passwords section like this:
'passwords' => [
'users' => [
'provider' => 'users',
'table' => env('AUTH_PASSWORD_RESET_TOKEN_TABLE', 'password_reset_tokens'),
'expire' => 60,
'throttle' => 60,
],
'guests' => [
'provider' => 'guests',
'table' => env('AUTH_PASSWORD_RESET_TOKEN_TABLE', 'password_reset_tokens'),
'expire' => 60,
'throttle' => 60,
],
],
Also, ensure your auth providers are correctly set up:
'providers' => [
'guests' => [
'driver' => 'eloquent',
'model' => App\Models\Guest::class,
],
],
Then, Apply this changes in your Auth/PasswordResetLinkController.php
public function store(Request $request): RedirectResponse
{
$request->validate([
'email' => ['required', 'email'],
]);
$status = Password::broker('guests')->sendResetLink( //add broker function in Password
$request->only('email')
);
return $status == Password::RESET_LINK_SENT
? back()->with('status', __($status))
: back()->withInput($request->only('email'))
->withErrors(['email' => __($status)]);
}
And in Auth\NewPasswordController
public function store(Request $request): RedirectResponse
{
$request->validate([
'token' => ['required'],
'email' => ['required', 'email'],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);
$status = Password::broker('guests')->reset( //add broker function to point to your guest passwords provider
$request->only('email', 'password', 'password_confirmation', 'token'),
function (Guest $user) use ($request) { // dont forget about what model you are using here
$user->forceFill([
'password' => Hash::make($request->password),
'remember_token' => Str::random(60),
])->save();
event(new PasswordReset($user));
}
);
}
Now Laravel will know to send the reset password link to a guest, and look them up using the guests table and Guest model.
đ Thatâs It!
With these small tweaks, your Laravel app is now fully compatible with custom-authenticated modelsâcomplete with password reset support.
Top comments (0)