tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen

Home Page:https://jwt-auth.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

401 Unauthorized but passing valid token

raultilves opened this issue · comments

Subject of the issue

401 Error but passing valid token in shared hosting.

Your environment

Q A
Bug? no
New Feature? no
Framework Laravel
Framework version 6.2
Package version 1.
PHP version 7.2

Steps to reproduce

Following the docs of jwt-auth

Expected behaviour

Access granted to the API

Actual behaviour

Response with 401 Unauthorized, even when the login has returned a valid token

This problem is solved for me but I'd like to help others that may be facing this issue.
If you have followed the docs, and you can access to login route or unprotected routes, but when trying to access protected routes you get the error 401, check with phpinfo() your PHP Variables, specially the HTTP_AUTHORIZATION (you can Ctrl+F and search).

If there is no HTTP_AUTHORIZATION var, this means you web server is not processing the Authorization header, wich is necesary for jwt-auth. You can enable this in your apache or nginx configuration, or just adding these two lines to your .htaccess file inside the root folder of your Laravel project:

RewriteEngine On (only add this line if you dont have it already in you htaccess file)
RewriteCond %{HTTP:Authorization} ^(.)
RewriteRule .
- [e=HTTP_AUTHORIZATION:%1]

This config will accept "Authorization" headers so JWT will be able to read your token.

I don't know why there are some shared hostings and even VPS with this configuration disabled by default. Good luck solving your issue, I hope I did help you.