Skip to content

Authentication & databases

loginAs / logout

php
$user = User::factory()->create();

$this->browse(function (Browser $browser) use ($user) {
    $browser->loginAs($user)          // or loginAs($user->id), loginAs('taylor@laravel.com')
        ->assertAuthenticated()
        ->assertAuthenticatedAs($user)
        ->visit('/dashboard')
        ->logout()
        ->assertGuest();
});

Dawn uses the same out-of-process mechanism as Dusk: the service provider registers /_dawn/login/{userId}/{guard?}, /_dawn/logout/{guard?} and /_dawn/user/{guard?} routes behind the web middleware group, gated to non-production environments. loginAs() simply visits the login endpoint in the real browser, so the session cookie is set exactly as it would be for a real user.

Configuration mirrors Dusk's (config/dawn.php keys path, domain, middleware - defaults _dawn, null, web).

Browser::login() (no arguments) resolves the default user through the user() method you can override on your test case.

Databases

The browser talks to your application over HTTP in a separate PHP process. That process cannot see a database transaction opened by your test process, which is why (exactly like Dusk):

  • DatabaseMigrations - works unchanged;
  • DatabaseTruncation - works unchanged;
  • RefreshDatabase - cannot work. Dawn fails fast in setUp() with a clear message instead of letting your suite fail mysteriously.

Released under the MIT License. Not affiliated with Laravel.