I am new in Laravel, I installed JWT and logged In , so It worked and generated a token, When I Logout in postman It returns true but again and again it returns true and
auth()->user()
always returns the user after logout
this is my code:
public function login(Request $request)
{
$this->validateLogin($request);
if (!$jwt_token = JWTAuth::attempt($request->toArray())) {
return response()->json([
'success' => false,
'message' => 'Invalid national_id or Password',
], 401);
}
return response()->json(['success' => true, 'token' => $jwt_token,], 200);
}
and in logout:
public function logout(Request $request)
{
auth()->logout();
return response()->json(['data' => 'you logged out successfully'],200)
}
In routes:
Route::group(['prefix' => 'v1', 'namespace' => 'Api\v1'], function() {
Route::post('login', 'Auth\LoginController@login');
});
Route::group(['middleware' => ['auth:api', 'api'], 'prefix' => 'v1', 'namespace' => 'Api\v1'], function() {
// Authentication Routes...
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
.
.
.
.
.
I also used JWTAuth::invalidate($request->token);
again it did not work.
i hope this will work for you
JWT is stateless, so token will be valid until it expires(You set the expiration). Either remove the token from your front end, or make a black list where you always check if the requested token is
valid
andnot black listed
.I found a method to do this in github