drhdjp97757 2018-08-11 07:13
浏览 499

在JWT中注销不起作用

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.

  • 写回答

2条回答 默认 最新

  • duanjuebiao6730 2018-08-11 08:13
    关注

    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 and not black listed.

    I found a method to do this in github

    public function testUserLogoutBlacklistsToken()
    {
        // Arrange
        $user = factory('App\Models\User')->create();
        $token = \Tymon\JWTAuth\Facades\JWTAuth::fromUser($user);
        $payload = \Tymon\JWTAuth\Facades\JWTAuth::getPayload($token);
        $headers = ['AUTHORIZATION' => 'Bearer ' . $token];
    
        // Assert
        $this->get('api/auth/logout', $headers)
             ->seeStatusCode(202)
             ->seeHeader('Authorization', '');
    
        // Verify on the back-end that the token is blacklisted
        $this->assertTrue(\Tymon\JWTAuth\Facades\JWTAuth::getBlacklist()->has($payload));
    }
    
    public function testAccessDeniedWithBlacklistedToken()
    {
        // Arrange
        $user = factory('App\Models\User')->create();
        $token = \Tymon\JWTAuth\Facades\JWTAuth::fromUser($user);
        \Tymon\JWTAuth\Facades\JWTAuth::invalidate($token);
    
         // Sanity check that JWTAuth::invalidate worked
         $this->assertTrue(\Tymon\JWTAuth\Facades\JWTAuth::getBlacklist()->has($payload));
    
        // User data should not be returned and response should have HTTP 500
        $this->get('api/me', $headers)
             ->seeStatusCode(500);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题