Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Leff7's avatar
Level 4

Laravel check if JWT token exists

I am using this package for token authentication in Laravel. In my controller I would like to check if the token exists

public function show(Article $article)
    {
        $article->media = $article->media;
        $article->externalMedia;

        if (JWTAuth token exists) {
            $this->addUserSpecificData($article);
        }


        return $article;
    }

But not sure how to do this?

0 likes
2 replies
gabrielbuzzi's avatar
JWTAuth::parseToken()->authenticate()

This way JWT will check if someone is auth.

And to check the cause of no valid token you can use try catch

TokenExpiraedException = Token is expired TokenInvalidException = Token is Invalid JWTException = Any other exception, when this is throwed is because token don't exist.

1 like

Please or to participate in this conversation.