dusk: Problems with using the loginAs
Environment: Homestead Laravel version: 5.5 Dusk version: 2.0
I have a problem that when running Laravel Dusk I cant use the loginAs method to log in as a user. When I do authentication using
->visit('/login')
->type('email', $user->email)
->type('password', 'password')
->press('Login')
it logs in the user correctly. But when I use ->loginAs($firstUser) that does not work.
I have checked that dusk goes to internal Dusk url when triggering loginAs
$this->visit(rtrim('/_dusk/login/'.$userId.'/'.$guard, '/'));
And that maps to a controller method
Route::get('/_dusk/login/{userId}/{guard?}', [
'middleware' => 'web',
'uses' => 'Laravel\Dusk\Http\Controllers\UserController@login',
]);
but method login is never actually run. How can I solve that?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 25 (3 by maintainers)
Closing this issue because it’s already solved, old or not relevant anymore. Feel free to reply if you’re still experiencing this issue.
Hi @driesvints, is there any config expected for dusk?
The first one works as it should be:
With this one:
I got:
@driesvints I just install Dusk 5.5.5 in an old laravelphp app updated to 6.0.3.
loginAsdoes not log in the given user.Do you see this route in your route list?
php artisan route:listDid you tried go to this address in your browser/_dusk/login/email/? Does it work? Can you put breakpoint toRoute::get('/_dusk/login/{userId}/{guard?}'....and go and see what is going on?TLDR: Add
APP_ENV=duskto your.env.dusk.localor.env.dusk.{environment}file since theDuskServiceProvideronly registers authentication/loginAs routes if theAPP_ENV!=production. The defaultAPP_ENVif not defined isproduction. See code link: https://github.com/laravel/dusk/blob/2eaa14a67ef9931fa13dab175f00a6bd7aad8236/src/DuskServiceProvider.php#L17You can probably set this through
phpunit.dusk.xmlas well, but if you don’t haveAPP_ENVset, you’ll get a 404 on those dusk routes:_dusk/login/{userId}/{guard?}_dusk/logout/{guard?}_dusk/user/{guard?}How I found out: I realized while running my tests in Laravel Sail, the Dusk runner will swap around your .env files. In that deep dive, I enabled video mode for my tests:
And added a pause in my tests:
And visited the video URL: http://192.168.214.5:4444/ui#/sessions and refreshed that screen when I ran the sail dusk --browse command.
The session will pop up, there will be a video icon, click on that, and you can enter the password:
secretand then view. what’s going on in your tests. I was getting a 404 on the login route: laravel.test/_dusk/login/1 was returning a404. Dove into source code to understand why those routes weren’t being registered and figured it was theAPP_ENVvalue.Hope this helps!
Seems to have to do with the fact that I am using cartalyst/sentinel for user authentication considering dusk uses laravel’s auth.
UPDATE
Dusk’s UserController always uses database set in .env and not .env.dusk.local but test cases do, using mysql.
This is what i have tried:
phpunit.xml
createApplication()
UPDATE 2
Managed to use my testing db by overriding route to use my own controller with \Sentinel::login($user), which works, but when a try $browser->visit any page I still get redirected to login.