Jwt-auth: The token could not be parsed from the request

Created on 1 Jun 2018  路  7Comments  路  Source: tymondesigns/jwt-auth

Lumen: 5.6
JWT: 1.0.0-rc.2
SO: Mac/Ubuntu

I'm making a simple API with Lumen, i use the JWTAuth library to make the tokens.

My login looks like:

                {
                    $this->validate($request, [
                        'email'    => 'required|email|max:255',
                        'password' => 'required'
                    ]);
                    try 
                    {
                        if (!$this->token = $this->jwt->attempt($request->only('email', 'password'))):
                            throw new HttpResponseException(response()->json(['message' => 'Email/Contrase帽a invalidos'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
                        endif;
                    } 
                    catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) 
                    {
                        throw new HttpResponseException(response()->json(['message' => 'Token expirado'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
                    }
                    catch (\Tymon\JWTAuth\Exceptions\TokenInvalidException $e) 
                    {
                        throw new HttpResponseException(response()->json(['message' => 'Token invalido'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
                    }
                    catch (\Tymon\JWTAuth\Exceptions\JWTException $e) 
                    {
                        throw new HttpResponseException(response()->json(['message' => 'Sin token'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
                    }
                    return response()->json([
                        "token" => $this->token,
                        "email" => $this->jwt->user()->email,
                        "rfc" => $this->jwt->user()->rfc,
                        "nombre" => $this->jwt->user()->nombre,
                        "_uof" => $this->Cipher->EncryptCipher($this->jwt->user()->id),
                        "_rof" => $this->Cipher->EncryptCipher($this->jwt->user()->rfc)
                    ], 200);
                }
                catch(\Exception $error)
                {
                    throw new HttpResponseException(response()->json(['message' => 'Error al iniciar sesi贸n el usuario'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
                }

That returns my token that is set on a React Frontend App, when i try to validate the token with curl or postman on my local enviroment with:

        {
            $this->payload = $this->jwt->getPayload($this->jwt->getToken())->toArray();
            var_dump($this->payload);
        }
        catch(\Exception $error)
        {
            var_dump($error->getMessage());
        }

My request looks like:

```
curl -X POST \
http://localhost:8000/debtor/validateLogin/ \
-H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9hcGltcGFsYS5yZWFsaG9zdC5jbG91ZFwvYXV0aFwvbG9naW4iLCJpYXQiOjE1MjcyODcyMTEsImV4cCI6MTUyNzI5MDgxMSwibmJmIjoxNTI3Mjg3MjExLCJqdGkiOiJyc3duZFh0NndzMXdhRGRvIiwic3ViIjoxLCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIiwibmFtZSI6IlVsaXNlcyBDYXJyZW9uIiwicmZjIjoiWEFYWDAxMDEwMTAwMCIsImF2YXRhciI6bnVsbH0.Ey1lMFYtSAE5f8pSrXjxnrMLGv2yOhl3DvwJo1qiTCI' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-H 'Postman-Token: 29819986-830d-48f8-8724-25ce2fbf0af2' \
-d '{}'

The token is on the Header of the request but when i make the request it throw this error "A token is required".

I've change the .htaccess to :



Options -MultiViews -Indexes

RewriteEngine On

# Handle Authorization Header
#RewriteCond %{HTTP:Authorization} .
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Fix authentication headers

RewriteCond %{HTTP:Authorization} ^(.)
RewriteRule .
- [e=HTTP_AUTHORIZATION:%1]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

```
But still doesn't work, any advice?

Regards

stale

Most helpful comment

Same here.
tymon/jwt-auth/src/JWTGuard.php

protected function requireToken()
{
    if (! $this->jwt->setRequest($this->getRequest())->getToken()) {
        throw new JWTException('Token could not be parsed from the request.');
    }

    return $this->jwt;
}

All 7 comments

I have the same issue on my configuration. Is there some fix for this?

Same here.
tymon/jwt-auth/src/JWTGuard.php

protected function requireToken()
{
    if (! $this->jwt->setRequest($this->getRequest())->getToken()) {
        throw new JWTException('Token could not be parsed from the request.');
    }

    return $this->jwt;
}

Same here.

My trace problem if below....

image

Did you manage to find a solution for this? I am getting the same error, just for 1 selected users over and over again.

Same here, there is no way to handle this Exception and redirects user to login page?

My two cents I changed logout function to this and call it with GET method

public function logout(Request $request) { if (! $user = auth()->setRequest($request)->user()) { return $this->responseUnauthorized(); } auth()->logout(); return $this->responseSuccess('Successfully logged out.'); }

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

Related issues

mihailo-misic picture mihailo-misic  路  3Comments

therealmjk picture therealmjk  路  3Comments

agneshoving picture agneshoving  路  3Comments

heroghost picture heroghost  路  3Comments

Rasoul-Karimi picture Rasoul-Karimi  路  3Comments