tymondesigns / jwt-auth

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

UnauthorizedHttpException (Token not provided) #1632

Open virajkaulkar opened 6 years ago

virajkaulkar commented 6 years ago

Subject of the issue

UnauthorizedHttpException Not Handled

Your environment

Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 5.4.y
Package version 1.x.y
PHP version 7.x.y

Steps to reproduce

Once I use middleware to guard my routes its throwing exception without handling it. It's Showing correct message but exception is not handled.

Expected behaviour

Exception should be handled and it should return error message in JSON format

Actual behaviour

It's throwing an UnauthorizedHttpException

dev4ndy commented 6 years ago

same problem.

Hi, I have the same problem as {'error': 'token_not_provided'}. I'm using Laravel 5.6 with "tymon / jwt-auth": "^ 0.5.12", everything worked fine on my localhost, but when I uploaded it to the hosting now I get the error mentioned above. In the headers I am sending the token, and I already change the .htaccess as indicated in others thread, but even so it does not work for me. Here are the headlines: `Response Headers

Access-Control-Allow-Origin: https://www.********.com Access-Control-Expose-Headers: Authorization Cache-Control: no-cache, private Connection: close Content-Type: application/json Date: Sat, 21 Jul 2018 03:20:18 GMT Server: Apache Transfer-Encoding: chunked Vary: Origin X-Powered-By: PHP/7.2.7 X-RateLimit-Limit: 60 X-RateLimit-Remaining: 58

Request headers

Accept: application/json Accept-Encoding: gzip, deflate, br Accept-Language: es-CO,en-US;q=0.7,en;q=0.3 Access-Control-Allow-Headers: Origin, X-Requested-With, Cont…t-Type, Accept, Authorization Authorization: Bearer eyJ0....... Cache-Control: no-cache Connection: keep-alive Content-Type: application/json Host: **.com Origin: https://www.**********.com Pragma: no-cache Referer: https://www.********.com/FrontAppDomicilio/ User-Agent: Mozilla/5.0 (X11; Linux x86_64…) Gecko/20100101 Firefox/61.0`

jprobert commented 6 years ago

Got the same problem, any fix?

Nischal-shrestha commented 6 years ago

You can try this in app/exceptions/Handler.php

Make sure you use these on top

use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
use Tymon\JWTAuth\Exceptions\TokenBlacklistedException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

And Inside the render method

public function render($request, Exception $exception)
    {
        // detect instance
        if ($exception instanceof UnauthorizedHttpException) {
            // detect previous instance
            if ($exception->getPrevious() instanceof TokenExpiredException) {
                return response()->json(['error' => 'TOKEN_EXPIRED'], $exception->getStatusCode());
            } else if ($exception->getPrevious() instanceof TokenInvalidException) {
                return response()->json(['error' => 'TOKEN_INVALID'], $exception->getStatusCode());
            } else if ($exception->getPrevious() instanceof TokenBlacklistedException) {
                return response()->json(['error' => 'TOKEN_BLACKLISTED'], $exception->getStatusCode());
            } else {
                return response()->json(['error' => "UNAUTHORIZED_REQUEST"], 401);
            }
        }
        return parent::render($request, $exception);
    }
narendrachoudhary1995 commented 5 years ago

its giving error UNAUTHORIZED_REQUEST

kilvn commented 5 years ago

very good

VinayKumarMedikonda commented 5 years ago

Simply, Change the request type to "post" and pass the "{token: (jwt auth token)}"

aktechtalks commented 5 years ago

thanks its working but its getting as a parameter not header?

noperman commented 4 years ago

its giving error UNAUTHORIZED_REQUEST

add params ?token="your_api_key"

polipones commented 4 years ago

Hi, my problem was, that Apache by default removes Auth header :man_facepalming:. Found fix here: https://github.com/tymondesigns/jwt-auth/issues/81#issuecomment-91782074

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.