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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)