yii2-jwt icon indicating copy to clipboard operation
yii2-jwt copied to clipboard

Returns error code 500 instead of 401 when token is expired

Open chertvazme opened this issue 2 years ago • 2 comments

Version 4.0-beta returns error code 500 instead of 401 when token is expired: The token violates some mandatory constraints, details:

  • The token is expired

chertvazme avatar Jan 07 '23 06:01 chertvazme

To fix this, create a new file called TokenExpiredFilter.php in your filters directory (just create a folder called 'filters' in your project directory)

Then add this code into the file: https://gist.github.com/cozumel424/02461064e5d227a031d26bddaf9322c9

And then in your base controller:

//add this above (so it runs first) the Jwt code
$behaviors['tokenExpiredFilter'] = [
      'class' => 'app\filters\TokenExpiredFilter',
    ];

    $behaviors['authenticator'] = [
      'class' => JwtHttpBearerAuth::class,
    ];

cozumel424 avatar Jul 04 '23 16:07 cozumel424