2016-08-19 58 views
2

我使用流明(5.2)框架和jwt(1.0)。如何使用流明刷新jwt中的令牌?

我得到一個令牌但我無法刷新它,因爲系統告訴我「該令牌已被列入黑名單」。我不知道如何解決它。你可以幫我嗎。

原諒我,我的英文不是很好,可能會有一些表達上的差異。

路線

$app->group(['prefix' => 'auth', 'namespace' => '\App\Http\Controllers'], function() use ($app) { 
    $app->post('/signin', '[email protected]'); 
    $app->put('/refresh', ['middleware' => ['before' => 'jwt.auth', 'after' => 'jwt.refresh'], 'uses' => '[email protected]']); 
}); 

登錄

public function signin(Request $request) 
{ 
    $this->validate($request, [ 
     'email' => 'required|email|max:255', 
     'password' => 'required' 
    ]); 

    try { 

     if ($token = $this->jwt->attempt($request->only(['email', 'password']))) { 
      return $this->json([ 
       'token' => $token 
      ]); 
     } 

     return $this->json([], 403, $this->_lang['signin_incorrect']); 

    } catch (JWTException $e) { 
     return $this->json([], 500, $e->getMessage()); 
    } 

} 

刷新

public function refresh() 
{ 

    try { 
     $this->jwt->setToken($this->jwt->getToken()); 

     if($this->jwt->invalidate()) { 
      return $this->json([ 
       'token' => $this->jwt->refresh() 
      ]); 
     } 

     return $this->json([], 403, $this->_lang['token_incorrect']); 


    } catch (JWTException $e) { 
     return $this->json([], 500, $e->getMessage()); 
    } 
} 

驗證服務提供商

public function boot() 
{ 
    // Here you may define how you wish users to be authenticated for your Lumen 
    // application. The callback which receives the incoming request instance 
    // should return either a User instance or null. You're free to obtain 
    // the User instance via an API token or any other method necessary. 

    $this->app['auth']->viaRequest('api', function ($request) 
    { 
     return \App\Models\User::where('email', $request->input('email'))->first(); 
    }); 
} 

回答

1

我已經解決了這個問題。

先我刪除jwt.refresh中間件。然後我使用JWT MANAGER刷新我的令牌。

這是現在的代碼

$app->group(['prefix' => 'auth', 'namespace' => '\App\Http\Controllers'], function() use ($app) { 

    $app->post('/signin', '[email protected]'); 
    $app->put('/refresh', '[email protected]'); 

}); 

控制器

return $this->json([ 
      'token' => $this->manager->refresh($this->jwt->getToken())->get() 
     ]); 
+0

我可以知道你使用的是什麼版本的智威湯遜的?我有同樣的問題,並做了這個,但它仍然存在。我正在使用'1.0.0-beta.3' – basagabi