Hi everyone!
I've created a simple SPA using Sanctum. The doc says /docs/8.x/sanctum#spa-authentication:
For this feature, Sanctum does not use tokens of any kind. Instead, Sanctum uses Laravel's built-in cookie based session authentication services.
And also
You are free to write your own /login endpoint; however, you should ensure that it authenticates the user using the standard, session based authentication services that Laravel provides. Typically, this means using the web authentication guard.
So I'm using Auth::attempt($request->validated()) in my api/login endpoint and I see it creates session
Illuminate\Session\Store {#306
#id: "nIiSSbsm2Weve9lPlzvbHKphyB55cjmSFICxugIm"
#name: "laravel_session"
#attributes: array:5 [
"_token" => "KdTqsOL06RW5T7cDiIuMaPb4DKVWfFaAbqCFK9ZK"
"_flash" => array:2 [
"old" => []
"new" => []
]
"_previous" => array:1 [
"url" => "http://laravel-artem-test.com/login"
]
"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d" => 6
"url" => array:1 [
"intended" => "http://laravel-artem-test.com/home"
]
]
#handler: Illuminate\Session\FileSessionHandler {#305
#files: Illuminate\Filesystem\Filesystem {#163}
#path: "/home/vagrant/code/laravel-test/storage/framework/sessions"
#minutes: "120"
}
#started: true
}
In my api/logout endpoint I'm also using standard laravel logout and it works properly
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
$request->session() shows
Illuminate\Session\Store {#309
#id: "qmzrqGplh9O8uT9chcKqbudIZ2fGvcU1WyJkSYch"
#name: "laravel_session"
#attributes: array:1 [
"_token" => "FJcXqGlSE9k9SY1csWPjTVSS5OmBuFqFO1JHokRA"
]
#handler: Illuminate\Session\FileSessionHandler {#308
#files: Illuminate\Filesystem\Filesystem {#163}
#path: "/home/vagrant/code/laravel-test/storage/framework/sessions"
#minutes: "120"
}
#started: true
}
But I want to do feature test for logout endpoint and here I faced the issue. My test method:
public function test_logout()
{
$user = User::factory()->create();
Sanctum::actingAs($user, [], 'web');
$response = $this->postJson(route('api.logout'));
$response->assertStatus(200)
->assertJsonCount(1)
->assertJson(fn (AssertableJson $json) =>
$json->has('redirect')
->whereType('redirect', 'string')
);
$this->assertGuest();
}
I'm getting
RuntimeException: Session store not set on request. in /home/vagrant/code/laravel-test/vendor/laravel/framework/src/Illuminate/Http/Request.php:515
and $response->ddSession(); gives me []
As I understand Sanctum::actingAs($user, [], 'web'); doesn't create session. But event if I use $this->actingAs($user, [], 'web')->withSession(['test' => true]) I still get [] and the same error occures.
I've already googled a lot but couldn't find any solution. Maybe someone has some thoughts? Thanks