Laravel 11: Custom Password Broker Ignores My Tenant-Aware Repository
I'm working on a Laravel 11 project where I have multiple tenants, and I'm trying to create a custom password broker that is tenant-aware. However, my custom repository is not being used, and Laravel is still using the default one.
Here's a breakdown of what I've done so far:
- Created a new service provider
TenantPasswordServiceProvider.phpthat extendsIlluminate\Auth\AuthServiceProvider. - Registered my custom password broker
TenantPasswordBroker.phpin theregistermethod of the provider. - Created a new repository
TenantUserRepository.phpthat extendsIlluminate\Database\Eloquent\Repositoryand is responsible for handling tenant-specific user queries. - Set the
modelproperty of the custom password broker to use the new repository. - In the
bootmethod of the provider, I've added the following code to bind the custom password broker to theAuthfacade:
$this->app->when(Auth::class)
->needs(PasswordBroker::class)
->give(function () {
return app(TenantPasswordBroker::class);
});
- I've also added the following code to the
registermethod of the provider to bind the custom repository to theAuthfacade:
$this->app->when(Auth::class)
->needs(TenantUserRepository::class)
->give(function () {
return app(TenantUserRepository::class);
});
- In my
config/app.phpfile, I've added the following line to theprovidersarray to register the new service provider:
'App\Providers\TenantPasswordServiceProvider',
- I've also added the following line to the
aliasesarray to alias the custom password broker:
'TenantPasswordBroker' => App\Providers\TenantPasswordBroker::class,
Despite all these changes, Laravel is still using the default password broker and repository. When I try to reset a password for a user, I get an error saying that the users table does not have a password column, which is the default table and column used by Laravel for authentication.
I've also tried using the Auth::provider method to manually set the password broker, but that didn't work either.
Any ideas on what I might be missing or doing wrong? Thanks in advance for any help!
First, it's important to note that Laravel 11 does not exist, so it's likely that you meant to say Laravel 8 or Laravel 9. I'll assume you meant Laravel 9 for the rest of this answer.
Based on the information you've provided, it seems that you're on the right track with creating a custom password broker and repository. However, there are a few things you might want to check:
- Make sure that your custom password broker and repository are being registered correctly. You can check this by running the
php artisan listcommand and looking for your custom broker and repository in the output. - Make sure that your custom password broker is being used when you call the
passwordmethod on theAuthfacade. You can check this by adding some debugging statements to your custom password broker to see if it's being instantiated when you callpassword(). - Make sure that your custom repository is being used when you call the
findUserByEmailmethod on your custom password broker. This method is responsible for finding a user by their email address, and it's what Laravel uses to validate password reset tokens. You can override this method in your custom password broker to use your custom repository instead. - Make sure that your custom repository is implementing the
findForPasswordResetmethod, which is used to find a user by their id or email address and the given password reset token. This method should return the user if the token is valid and the user exists, or null if the token is invalid or the user does not exist. - Make sure that your
config/app.phpfile is correctly configured to use your custom password broker. You should set thepasswordskey in theautharray to point to your custom password broker. For example:
'auth' => [
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'passwords' => [
'provider' => 'TenantPasswordBroker',
],
],
- Finally, make sure that your tenant-specific users are being stored in a database table that is being used by Laravel's authentication system. By default, Laravel uses the
userstable, but you can change this by setting the$tableproperty on yourUsermodel.
I hope this helps! Let me know if you have any further questions or if there's anything else I can do to help.