Salar
Salar

Reputation: 5509

Laravel check if a jwt token is expired or invalid using tymondesigns

I'm using laravel and tymondesigns for handling JWT tokens. I want to check manually if a provided token is expired or invalid. which functions should I use?

Upvotes: 0

Views: 8685

Answers (2)

Amaar Hassan
Amaar Hassan

Reputation: 378

Dont know if it is valid for your case or not but i exposed a simple method to do so:

public function isValidToken(Request $request)
{
    return response()->json(['valid' => auth()->check()]);
}

Upvotes: 0

SP Singh
SP Singh

Reputation: 438

There is not as such direct method to do so. You can do the following to know about token status.

 try {
          JWTAuth::parseToken()->authenticate();
    } catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {

        // do whatever you want to do if a token is expired

    } catch (\Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {

        // do whatever you want to do if a token is invalid

    } catch (\Tymon\JWTAuth\Exceptions\JWTException $e) {

        // do whatever you want to do if a token is not present
    }

The only way at this state is that catch an exception for the token state and perform necessary action. Put it into common service to return token status

Upvotes: 6

Related Questions