Jwt-auth: Small sample for exception handling

Created on 30 Jan 2018  路  5Comments  路  Source: tymondesigns/jwt-auth

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!

stale

Most helpful comment

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);
    }

All 5 comments

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

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();
            }
        }

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);
    }

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

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.

Was this page helpful?
0 / 5 - 0 ratings