Search code examples
laraveltddlaravel-passportlaravel-dusk

Laravel Dusk auth issues with Laravel Passport


Can't test applications with Laravel Dusk if the app uses Laravel Passport for authentication. It's always 404 and if you stop the test with ->stop() it starts working.

The app is located: localhost:3000 and all requests are proxied to api.example.test

I've tried to authenticate the user inside the test:

$this->actingAs(User::find(1), 'api');
$browser->loginAs(User::find(1));
Passport::actingAs($this->user);

But still the same.

That's not the only problem, I think the main issue is also that I can't log in with Dusk by filling out the form and click "login"

$browser->visit('/login')
   ->type('@email', 'john@doe.com')
   ->type('@password', 'password')
   ->click('@login-button')
   ->waitForLocation('/');

But again, if you stop the test with ->stop() you can manually click the button and it will log you in.

Is Laravel Dusk useless if your app does not use basic auth?


Solution

  • Solved.

    The problem was that my Dusk .env file had APP_URL=localhost:3000 where the front-end is located, but my APIs .env file had APP_URL=api.exmaple.com and that's why in my AuthController, I issued the token from env('APP_URL').'/oauth/token' <- which resulted in Dusk env to localhost:3000/oauth/token which is ofc 404

    I changed that to env('API_URL') and added that value to my both .env files.