Jwt-auth: Token can't be decoded - Laravel 5.5

Created on 13 Oct 2017  路  10Comments  路  Source: tymondesigns/jwt-auth

I'm setting up an authentication route with my API.
I am using laravel 5.5 with 1.0.0-rc.1 and Postman to interact with the API.

The authentication route/method seems to work:

    /**
     * Authenticates a json request, generating a token.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function authenticate(Request $request)
    {
        // grab credentials from the request
        $credentials = $request->only('email', 'password');

        try {
            // attempt to verify the credentials and create a token for the user
            if (! $token = JWTAuth::attempt($credentials)) {
                return response()->json(
                    [
                        'error' => 'Invalid credentials.',
                        'detail' => 'Please use your email and password to generate a token.'
                    ],
                    401);
            }
        } catch (JWTException $e) {
            // something went wrong whilst attempting to encode the token
            return response()->json(
                [
                    'error' => 'Could not create token',
                    'detail' => 'There was an internal problem and your token could not be created.'
                ], 500
            );
        }

        // all good so return the token
        return response()->json(compact('token'));
    }

A Postman API post request returns (what seems to be) a valid response, For example:

{
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vc29sZGVyc3RhcmFwaS5jb20ubG9jYWwvYXBpL2F1dGhlbnRpY2F0ZSIsImlhdCI6MTUwNzg4NjU2OSwiZXhwIjoxNTA3ODkwMTY5LCJuYmYiOjE1MDc4ODY1NjksImp0aSI6IkpFWjBkc0dNbEVydXRHcFciLCJzdWIiOiIwNzk2MjhDMC03QjBDLTExRTYtODRERC1DQjAzMzVGN0JBNUQiLCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.Dl2EEaYZx3H5XXG9WUcPXYKuma0ZjCvcCsb99hgB6O4"
}

To begin with, for basic testing purposes, I am feeding this to an action using GET, with the following suffix:

?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vc29sZGVyc3RhcmFwaS5jb20ubG9jYWwvYXBpL2F1dGhlbnRpY2F0ZSIsImlhdCI6MTUwNzg4NjU2OSwiZXhwIjoxNTA3ODkwMTY5LCJuYmYiOjE1MDc4ODY1NjksImp0aSI6IkpFWjBkc0dNbEVydXRHcFciLCJzdWIiOiIwNzk2MjhDMC03QjBDLTExRTYtODRERC1DQjAzMzVGN0JBNUQiLCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.Dl2EEaYZx3H5XXG9WUcPXYKuma0ZjCvcCsb99hgB6O4

In order to test this, if I do the following:

public function globalObjects(Request $request): JsonResponse {
    var_dump(JWTAuth::parseToken()->authenticate(), JWTAuth::getToken()); exit;

   // ... later code that never gets reached
}

I get the following:

bool(false) object(Tymon\JWTAuth\Token)#809 (1) { ["value":"Tymon\JWTAuth\Token":private]=> string(384) "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vc29sZGVyc3RhcmFwaS5jb20ubG9jYWwvYXBpL2F1dGhlbnRpY2F0ZSIsImlhdCI6MTUwNzg4NjU2OSwiZXhwIjoxNTA3ODkwMTY5LCJuYmYiOjE1MDc4ODY1NjksImp0aSI6IkpFWjBkc0dNbEVydXRHcFciLCJzdWIiOiIwNzk2MjhDMC03QjBDLTExRTYtODRERC1DQjAzMzVGN0JBNUQiLCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.Dl2EEaYZx3H5XXG9WUcPXYKuma0ZjCvcCsb99hgB6O4" }

.. as in:

  • I receive the token
  • it does not find the user

Is this a coding issue, or is it a bug? I see no-one else with this problem, so I'm hoping it's simply my code.

Thanks
Rick

stale

Most helpful comment

Here is my way to obtain user, I did't remember why, but my parseToken() fails, so I use other option:

$user = JWTAuth::setToken($request->token)->toUser();

All 10 comments

There's been no response for 5 days, so I'm adding this to Stack Overflow.
If you want the SO reputation there, I'll mark correct answers and copy back to here.

Here is my way to obtain user, I did't remember why, but my parseToken() fails, so I use other option:

$user = JWTAuth::setToken($request->token)->toUser();

Thanks, I still get bool(false) with a freshly generated token.

My only thought about this is that I am using a UUID, not a numeric ID. Could this be a problem?

Possible clue: the UUID is a binary(16).
Thus, it may have "text based transmission" problems. For example, I use a translated UUID (id_text) which is a text representation of this when using forms.

Is there a way of using another unique field, for example, email?

Update: I found this, but it didn't help:

I set the identifier to be:

'identifier' => 'email',

And it still returns false.

I have raised this on stack overflow here:
https://stackoverflow.com/questions/46809043/laravel-tymondesigns-jwt-auth-problems/46819476#46819476

You will notice that I have found the problem, and a possible solution.

  • tymon\jwt-suth\src\JWTAuth::authenticate uses id, even with 'identifier' => 'email' set

So. It turns out I need to override this. I have created the following ServiceProvider:
class JWTUuidAuthServiceProvider extends JWTAuth { /** * Authenticate a user via a token. * * @return \Tymon\JWTAuth\Contracts\JWTSubject|false */ public function authenticate() { // my correctly functioning custom version of authenticate() return $this->user(); } }
And I have modified app.php to be:
'JWTAuth' => App\Providers\JWTUuidAuthServiceProvider::class,
.. However this still ends up with the old class being used.

How can I make laravel use the new extended class?

i think that is better if you check 1.0 documentation.
Tymon suggest to use laravel auth handling. so Auth::guard for generate the token. (setting on guard, driver jwt)

Here is my way to obtain user, I did't remember why, but my parseToken() fails, so I use other option:

$user = JWTAuth::setToken($request->token)->toUser();

thankyou working perfactly

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.

Closing as no longer relevant.

Was this page helpful?
0 / 5 - 0 ratings