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

401 Unauthorized but passing valid token #2216

Open
raultilves opened this issue Mar 28, 2023 · 0 comments
Open

401 Unauthorized but passing valid token #2216

raultilves opened this issue Mar 28, 2023 · 0 comments

Comments

@raultilves
Copy link

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.

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

1 participant