Search code examples
phpjwtlumen

JWT Auth token not invalidating after logout in Lumen 5.4


I had a fresh Lumen 5.4 installation and followed this tutorial. Login and others work fine but the logout doesn't seem to work properly. What I mean is, if I try to expire a token it doesn't give me an error but if the same token(the one that was just expired) is re-used, it should say expired but still goes through and gets me the data. In simple terms, I believe it is not expiring the token at all. Below is my code:

UserController code:

class UserController extends Controller
{
    protected $jwt;
    public function __construct(JWTAuth $jwt)
    {
            $this->jwt = $jwt;
    }

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

        if (!$token = $this->jwt->attempt($request->only('email', 'password'))) {
            return response()->json(['The credentials provided are invalid.'], 500);
        }

        return response()->json(compact('token'));
    }

    public function LogoutUser(Request $request){
        $this->jwt->invalidate($this->jwt->getToken());

        return response()->json([
            'message' => 'User logged off successfully!'
        ], 200);
    }
}

routes:

$app->group(['prefix' => 'api'], function($app){
    $app->post('/signup', [
        'uses' => 'UserController@Signup'
    ]);

    $app->group(['middleware' => 'auth:api'], function($app){
        $app->post('/logout',[
            'uses' => 'UserController@LogoutUser'
    ]);
    });
});

config/auth.php:

'defaults' => [
    'guard' => env('AUTH_GUARD', 'api'),
],
'guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users'
    ],
],
'providers' => [
    'users' => [
       'driver' => 'eloquent',
        'model'  => \App\User::class,
    ],
],
'passwords' => [
    //
],

Any help will be greatly appreciated.


Solution

  • I have now got it working and leave behind the steps so if anybody else faces the same issue. The fix was to use CACHE_DRIVER=file in the .env file. I am not exactly sure why or how this fixes it but some research led me to this and was a result of trial and error things.