Sven Nijkamp
Sven Nijkamp

Reputation: 71

Laravel Passport gives me "Invalid key supplied"

Im am trying to make login functionality using Laravel 11, Laravel Passport and TanancyForLaravel, but i get the following error: Invalid key supplied I want to create login functionality for users at a tenant. Each tenant has its own set of users, and they should only be able to login by their tenant. Each tenant has its own database. My guess is that Laravel is looking for keys in the main database, while it should be looking for the keys in my tenant's database.

I have the following config/auth.php:

'guards' => [
        'tenant_web' => [
            'driver' => 'session',
            'provider' => 'tenant_users',
        ],
        'tenant' => [
            'driver' => 'passport',
            'model' => 'tenants_users',
        ],
    ],

'providers' => [
        'main_users' => [
            'driver' => 'eloquent',
            'model' => env('AUTH_MODEL', App\Models\User::class),
        ],

         'tenant_users' => [
             'driver' => 'tenant',
             'model' => App\Models\Tenant\User::class,
         ],
    ],

And this route in my Controllers/AuthController.php:

    public function store(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);

        $success = auth()->guard('tenant')->attempt([ //This is where the error is thrown
                'email' => $request->input('email'),
                'password' => $request->input('password'),
            ]);

        if ($success) {
            $token = auth()->user()->createToken('authToken') //Error is also thrown here
            ->accessToken; 
            $response = [
                'success' => true,
                'token' => $token
            ];
            return response($response, 200);
        } else {
            $response = ["message" => "Password mismatch"];
            return response($response, 422);
        }
    }

This route is called here:

Route::middleware([
    'api',
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {
    Route::post('/login', [AuthController::class, 'store']);
});

I have tried lots of different solutions, followed guides, and tried everything i could think of. The main guide I've been following is: text Which says this:

Note: Don't use the passport:install command. The command creates the encryption keys & two clients in the central application. Instead of that, we'll generate the keys and create the clients manually later.

I then did the following:

To generate a single Passport key pair for the whole app, create Passport clients for your tenants by adding the following code to your tenant database seeder.

public function run()
{
    $client = new ClientRepository();

    $client->createPasswordGrantClient(null, 'Default password grant client', 'http://your.redirect.path');
    $client->createPersonalAccessClient(null, 'Default personal access client', 'http://your.redirect.path');
}

You can set your tenant database seeder class in config/tenancy.php file at seeder_parameters key. Then, seed the database and generate the key pair by running php artisan passport:keys.

If you need any more information, please ask because I really need an answer. Thanks for everyting!

Upvotes: 5

Views: 12562

Answers (1)

This works for me:

php artisan passport:install

after that you run this:

php artisan passport:keys --force

then:

sudo chmod -R 0777 ./storage 

Upvotes: 6

Related Questions