Am using Laravel Passport
to build an API, I removed the web routes and its guard accordingly
How can I test user logout?
This is what I have so far:
Logout Test
/**
* Assert users can logout
*
* @return void
*/
public function test_logout()
{
// $data->token_type = "Bearer"
// $data->access_token = "Long string that is a valid token stripped out for brevety"
$response = $this->json('POST', '/api/logout', [], [
'Authorization' => $data->token_type . ' ' . $data->access_token
]);
$response->assertStatus(200);
}
routes/api.php
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
The controller method uses the AuthenticatesUsers
trait so the default function is kept
/**
* Log the user out of the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->invalidate();
return $this->loggedOut($request) ?: redirect('/');
}
Error Method Illuminate\Auth\RequestGuard::logout does not exist
The Laravel Documentation talks about issuing and refreshing access tokens but nothing about revoking them or performing logout
Note: am using password grant tokens
Note 2: revoking the user's token doesn't work
public function logout(Request $request)
{
$request->user()->token()->revoke();
return $this->loggedOut($request);
}
Test Fails on second assertion
public function test_logout()
{
$response = $this->json('POST', '/api/logout', [], [
'Authorization' => $data->token_type . ' ' . $data->access_token
]);
$response->assertStatus(200); // Passes
$check_request = $this->get('/api/user');
$check_request->assertForbidden(); // Fails
}
Given the default route requiring authentication
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Response status code [200] is not a forbidden status code.
So what's going on? and how can I test user logout with Passport?
$response->assertUnauthorized();
– Apterygial