tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.28k stars 1.55k forks source link

Small sample for exception handling #1468

Open prodigy7 opened 6 years ago

prodigy7 commented 6 years ago

After some hours I get my installation now working with multiple guards and laravel 5.5. Finally https://github.com/tymondesigns/jwt-auth/blob/develop/docs/laravel-installation.md helped me implement it on a way, its working. Now I want implement custom exception handling but the documentation (https://github.com/tymondesigns/jwt-auth/blob/develop/docs/exception-handling.md) is currently empty. Is there anyone, can provide me with a small working sample for 1.0.0-rc.1?

Thx!

yhylord commented 6 years ago

Second this. It's very annoying when there even isn't a list of possible exceptions.

jampack commented 6 years ago

if you want to handle the exceptions globally in handler.php

if ($exception instanceof UnauthorizedHttpException){
            if(get_parent_class($exception) instanceof TokenExpiredException){
                return JR::JWTTokenExpired();
            }elseif (get_parent_class($exception) instanceof TokenBlacklistedException){
                return JR::JWTTokenBlacklisted();
            }else{
                return JR::JWTTokenInvalid();
            }
        }
victorrss commented 6 years ago

I am using

"php": ">=7.0.0",
"fideloper/proxy": "~3.3",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0",
"tymon/jwt-auth": "1.0.0-rc.1"

it worked here Add the following code to the render method within app/Exceptions/Handler.php

public function render($request, Exception $e)
    {
        if($e instanceof \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException){
            return response()->json([$e->getMessage()], $e->getStatusCode());
        }
        return parent::render($request, $e);
    }
phalconVee commented 5 years ago

I am using

"php": ">=7.0.0",
"fideloper/proxy": "~3.3",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0",
"tymon/jwt-auth": "1.0.0-rc.1"

it worked here Add the following code to the render method within app/Exceptions/Handler.php

public function render($request, Exception $e)
    {
        if($e instanceof \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException){
            return response()->json([$e->getMessage()], $e->getStatusCode());
        }
        return parent::render($request, $e);
    }

This solved the issue for me

stale[bot] commented 3 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.