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

Returning custom response from events #6

Closed
cwhite92 opened this issue Oct 23, 2014 · 2 comments
Closed

Returning custom response from events #6

cwhite92 opened this issue Oct 23, 2014 · 2 comments

Comments

@cwhite92
Copy link

First, thank you for a great library! Made my life a lot simpler.

I'm attempting to return my own response when a user sends an invalid token in order to have my API consistent. I've created an events.php file under app/ and hooked into the appropriate event:

Event::listen('tymon.jwt.invalid', function()
{
    return Response::json([
        'errors' => ['Invalid authentication token']
    ], Config::get('status.error.unauthorized'));
});

However this doesn't seem to change anything, I'm still getting this response returned:

{"error":"token_invalid"}

I know the event is firing because if I die("test") inside it, I see test.

How would you recommend I return my own response?

@tymondesigns tymondesigns added bug and removed bug labels Oct 24, 2014
@tymondesigns
Copy link
Owner

After testing this myself, I can see that you are indeed correct. The event is fired but anything returned from the event is lost. Which is an oversight on my part :(

The reason is because the event is not being returned from the filter, so the response never 'goes anywhere'. I think I have a fix in mind for this, which I will try to implement at some point today.

For now, to get the custom responses you want, you could create your own filter in filters.php as follows:

Route::filter('custom-jwt', function ($route, $request)
{
        if ( ! $token = JWTAuth::getToken($request) )
        {
            return Response::json(['error' => 'token_not_provided'], 400);
        }

        try
        {
            $user = JWTAuth::toUser($token);
        }
        catch(Tymon\JWTAuth\Exceptions\TokenExpiredException $e)
        {
            return Response::json(['error' => 'token_expired'], 401);
        }
        catch(Tymon\JWTAuth\Exceptions\JWTException $e)
        {
            return Response::json(['error' => 'token_invalid'], 400);
        }

        if (! $user)
        {
            return Response::json(['error' => 'user_not_found'], 404);
        }
});

and replace those Responses with your own.

As soon as I have a fix for this, I will post it here.

Thanks!

@tymondesigns
Copy link
Owner

Issue is now resolved and tagged as a new release (0.3.6). Feel free to run composer update to pull this in 😄

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

No branches or pull requests

2 participants