Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw token expired error #17

Closed
abdouTurki opened this issue Mar 28, 2019 · 2 comments
Closed

throw token expired error #17

abdouTurki opened this issue Mar 28, 2019 · 2 comments

Comments

@abdouTurki
Copy link

Hi,
I followed the steps in the readme section, and it works. My only issue is that when the token is expired I get this error
{ "name": "Unauthorized", "message": "Your request was made with invalid credentials.", "code": 0, "status": 401, "type": "yii\\web\\UnauthorizedHttpException" }
I want to customize it to "token expired message" so in the frontend I can handle it and send a request to refresh the token.
do you any idea on how to achieve this?

@sizeg
Copy link
Owner

sizeg commented Mar 28, 2019

Hi @abdouTurki ,
by default exception is thrown by parent method handleFailure(). So you can extend JwtHttpBearerAuth and override it.

class MyJwtAuth extends \sizeg\jwt\JwtHttpBearerAuth
{
    /**
     * {@inheritdoc}
     */
    public function handleFailure($response)
    {
        // you can create any exception you wish and throw it here or customize response
        throw new UnauthorizedHttpException('token expired message');
    }
}

@rdhruval
Copy link

rdhruval commented Oct 1, 2021

@sizeg

I have the same problem, I want to handle the whole failed response.

{ "name": "Unauthorized", "message": "Your request was made with invalid credentials.", "code": 0, "status": 401, "type": "yii\\web\\UnauthorizedHttpException" }

But I want a custom response. something like below.
{ "status": "failed", "message": "Your message.", "data": [] }

I have checked the answer but don't get where to put this file.

class MyJwtAuth extends \sizeg\jwt\JwtHttpBearerAuth
{
    /**
     * {@inheritdoc}
     */
    public function handleFailure($response)
    {
        // you can create any exception you wish and throw it here or customize response
        throw new UnauthorizedHttpException('token expired message');
    }
}

can you please let me know how to achieve a custom response?

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants