2

I using lumen(5.2) framework and jwt(1.0).

I get a token but i can't refresh it ,because the system tell me "The token has been blacklisted". I don't know how solved it. could you please help me.

Forgive me, My English isn't very good, There may be some differences in expression.

Route

$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']);
});

Sign in

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());
    }

}

Refresh

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());
    }
}

Auth Service Provider

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();
    });
}
Du L.P
  • 31
  • 1
  • 3

2 Answers2

1

I have solved this problem.

first I removed jwt.refresh middleware. then I using JWT MANAGER to refresh my token.

this is now code

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

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

});

Controller

return $this->json([
            'token' => $this->manager->refresh($this->jwt->getToken())->get()
        ]);
Du L.P
  • 31
  • 1
  • 3
  • may I know what version of JWT you are using? I am having the same problem and did this as well but it still persists. I am using `1.0.0-beta.3` – basagabi Jul 31 '17 at 03:59
  • 1
    I am using lumen 8.0, will this solution work for this lumen version? – Kamlesh Apr 27 '21 at 06:38
0

add JWT_BLACKLIST_ENABLED=false in .env file