Reloading the app causes 401 Unauthorised on first api call after token is validated
See original GitHub issueI’m submitting a…
- [x ] Regression (a behavior that used to work and stopped working in a new release)
- [x ] Bug report
Current behavior
If an existing and logged in user:
- the user reloads the app using browser refresh,
- then the token is validated correctly,
- but the next call to the API fails as 401 Unauthorized.
- all following calls succeed
Expected behavior
First request after reloading the app (ie. browser refresh) should not result in 401.
Environment
Angular-Token version: 6.0.3 & 6.0.4 Angular version: 6.1.7 Rails 5.1.6 devise_token_auth 0.2.0 Bundler
- Angular CLI (Webpack)
Browser:
- Chrome (desktop) version XX
- Chrome (Android) version XX
- Chrome (iOS) version XX
- Firefox version XX
- Safari (desktop) version XX
- Safari (iOS) version XX
- IE version XX
- Edge version XX
Other After the validate_token is called and a new token is sent back by the API, that token is written to localstorage. But the next api call (to get data) does not use it, it is still using the previous token.
The Rails api throws the error “Filter chain halted as authenticate_user! redirected”.
The next api request succeeds.
What is also strange is that if I add this test method to see what happens in the Rails application_controller, I get the output shown by “->”:
before_action :test
respond_to :json
include Devise::Controllers::Helpers
include DeviseTokenAuth::Concerns::SetUserByToken
def test
puts '****'
puts request.env["HTTP_ACCESS_TOKEN"] -> shows the token (ie the old one)
puts request.env["access-token"] -> shows nothing
authenticate_user!
end
Issue Analytics
- State:
- Created 5 years ago
- Comments:8
Top Results From Across the Web
How to Quickly Fix the 401 Unauthorized Error (5 Methods)
The 401 (Unauthorized) status code indicates that the request has not been applied because it lacks valid authentication credentials for the ...
Read more >401 Unauthorized Error: What It Is and How to Fix It
The 401 Unauthorized Error is an HTTP response status code indicating that the client could not authenticate a request.
Read more >401 Error: 5 Ways to Troubleshoot and Fix It - Hostinger
The 401 Unauthorized error is triggered by unauthenticated requests made to a WordPress web server. Learn how to identify and fix the issue....
Read more >HTTP 401 Unauthorized Error | What Is and How to Fix?
401 Unauthorized Error is an HTTP reaction rating code illustrating that an application made by the client has not been verified. A network...
Read more >401 Unauthorized after a few hours of continued use of app
Auth tokens are most commonly invalidated because either the expiry time has elapsed or too many tokens were created (when a new one...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
All good @rmcsharry I found out I was actually encountering a different issue of my own creation.
I was setting
apiBase
programatically based on a value stored in local storage which wasn’t available before the first API call, so the API was correctly responding with a 401 as the token wasn’t present in the default tenant (multi-tenant app) forcing sign out and the user to login again.Once I fixed this all seems to be working fine, although perhaps I’m just yet to encounter your issue…
@SimonBrazell No, I never found a solution, my app still does this 👎