Laravel 11: Custom Password Broker Ignores My Tenant-Aware Repository

Updated: Feb 21, 2025

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:

  1. Created a new service provider TenantPasswordServiceProvider.php that extends Illuminate\Auth\AuthServiceProvider.
  2. Registered my custom password broker TenantPasswordBroker.php in the register method of the provider.
  3. Created a new repository TenantUserRepository.php that extends Illuminate\Database\Eloquent\Repository and is responsible for handling tenant-specific user queries.
  4. Set the model property of the custom password broker to use the new repository.
  5. In the boot method of the provider, I've added the following code to bind the custom password broker to the Auth facade:
$this->app->when(Auth::class)
    ->needs(PasswordBroker::class)
    ->give(function () {
        return app(TenantPasswordBroker::class);
    });
  1. I've also added the following code to the register method of the provider to bind the custom repository to the Auth facade:
$this->app->when(Auth::class)
    ->needs(TenantUserRepository::class)
    ->give(function () {
        return app(TenantUserRepository::class);
    });
  1. In my config/app.php file, I've added the following line to the providers array to register the new service provider:
'App\Providers\TenantPasswordServiceProvider',
  1. I've also added the following line to the aliases array 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:

  1. Make sure that your custom password broker and repository are being registered correctly. You can check this by running the php artisan list command and looking for your custom broker and repository in the output.
  2. Make sure that your custom password broker is being used when you call the password method on the Auth facade. You can check this by adding some debugging statements to your custom password broker to see if it's being instantiated when you call password().
  3. Make sure that your custom repository is being used when you call the findUserByEmail method 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.
  4. Make sure that your custom repository is implementing the findForPasswordReset method, 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.
  5. Make sure that your config/app.php file is correctly configured to use your custom password broker. You should set the passwords key in the auth array to point to your custom password broker. For example:
'auth' => [
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],

    'passwords' => [
        'provider' => 'TenantPasswordBroker',
    ],
],
  1. 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 users table, but you can change this by setting the $table property on your User model.

I hope this helps! Let me know if you have any further questions or if there's anything else I can do to help.