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

Invalidate expired token (but renewable)

Open dhcmega opened this issue 4 years ago • 4 comments

Subject of the issue

Invalidating a token gives token expired exception

Your environment

Q A
Bug? no (?)
New Feature? yes
Framework Laravel
Framework version 8.47
Package version 1.0.2
PHP version 8.0.1

Steps to reproduce

Try to invalidate an expired token, with: auth()->invalidate()

Expected behaviour

Token get blacklisted

Actual behaviour

TokenExpired exception thrown

thanks!

dhcmega avatar Jun 20 '21 19:06 dhcmega

Did you find a solution ? I'm having the same problem

Ademking avatar Jun 30 '21 19:06 Ademking

Hi, no. I found that refreshing the token invalidates the previous one. So I create and drop a new one: auth()->refresh(); If you have already created a new token, this will invalidate the new one.

dhcmega avatar Jun 30 '21 19:06 dhcmega

Hi, no. I found that refreshing the token invalidates the previous one. So I create and drop a new one: auth()->refresh(); If you have already created a new token, this will invalidate the new one.

Thank you @dhcmega This is my final code

        $old_token = $user->jwt_token;
        try {
            JWTAuth::setToken($old_token)->invalidate();
        } catch (TokenExpiredException $e) {
            // do nothing
        }

Ademking avatar Jun 30 '21 21:06 Ademking

Oh, ok, it throws an exception but also invalidates the token? Thanks for sharing.

dhcmega avatar Jun 30 '21 21:06 dhcmega