如何使用流明刷新jwt中的令牌?

时间:2016-08-19 04:23:15

标签: jwt lumen lumen-5.2

我使用流明(5.2)框架和jwt(1.0)。

我得到一个令牌,但我无法刷新它,因为系统告诉我“令牌已被列入黑名单”。我不知道怎么解决了。你能不能帮助我。

请原谅我,我的英语不是很好,表达方式可能有些不同。

路线

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

登录

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 个答案:

答案 0 :(得分:1)

我已经解决了这个问题。

首先我删除了jwt.refresh中间件。然后我使用JWT MANAGER刷新我的令牌。

这是现在的代码

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

    $app->post('/signin', 'AuthController@signin');
    $app->put('/refresh', 'AuthController@refresh');

});

控制器

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