Home > Blockchain >  How to test whether a user is logged in or not through laravel testing
How to test whether a user is logged in or not through laravel testing

Time:12-13

I am writing a test for logout API. Following is the controller method to logged a user out.

    $user = $request->user();
    $user->currentAccessToken()->delete();

I cannot assert $this->assertGuest(); because It is a single page application and I want to test that whether the user is logged in or not after hitting logout api.

CodePudding user response:

As per your query you wanted to test if a user is logged out or not. Also in your provided code you are testing it out by asserting it as guest. It will not work if you did not provided any guest routes and middleware. But in Laravel, you can use the Auth::check() method to determine if a user is logged in. If the user is not logged in, this method will return false. You can use it in a feature test like this :-

 public function testUserIsLoggedOut()
{
    $this->assertFalse(Auth::check());
}
  • Related