Try below code instead of ->actingAs($user)..
auth()->login($user);
$this->visit('/')->andSoOn()..
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
When a user has successfully authenticated, I am adding a session variable to make API calls. I want to be able to test this but I am not finding a way to do so. Here is what I have tried.
/** @test */
function a_users_api_token_is_stored_in_session_after_login()
{
$user = factory(User::class)->create();
$user = User::find($user->id);
$this->actingAs($user)
->visit('/')
->assertSessionHas('api_token');
}
This always comes back as false. However, when I manually log in and look at my session variables, it works as expected.
Anyone know how to do this?
Please or to participate in this conversation.