question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reloading the app causes 401 Unauthorised on first api call after token is validated

See original GitHub issue

I’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:

  1. the user reloads the app using browser refresh,
  2. then the token is validated correctly,
  3. but the next call to the API fails as 401 Unauthorized.
  4. 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:open
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
SimonBrazellcommented, Jan 27, 2021

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…

0reactions
rmcsharrycommented, Jan 26, 2021

@SimonBrazell No, I never found a solution, my app still does this 👎

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found