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.

Confused about sending authentication headers for token validation to API.

See original GitHub issue

Hello, I have a setup in which I have a rails 4 API having the gem devise_token_auth and hosted as a separate application so I have also rack-cors configured to handle cross origin requests. Using angular2-token on my front end Angular 2 applicaiton I have been able to successfully sign up and sign in as well as sign out users via my API. The issue however, which I have encountered occurs only when the user is signed in and upon refreshing the browser I get this error in the rails API console as well as in the browser, checked in firefox as well as chrome.

Started GET "/api/v1/auth/validate_token" for 127.0.0.1 at 2017-02-06 17:42:49 +0500
Processing by DeviseTokenAuth::TokenValidationsController#validate_token as JSON

followed by:

SELECT  "users".* FROM "users" WHERE "users"."uid" = $1 LIMIT 1  [["uid", "abc@xyz.com"]]
Completed 401 Unauthorized in 76ms (Views: 0.2ms | ActiveRecord: 0.3ms)

My initial assumption during the configuration of this package in my Angular2 app was that it will implicitly include authentication headers in each request. However after repeatedly going through the gem’s documentation I also added the headers myself when I initialize the token service in my app.component.ts file.

this._tokenService.init({
     apiPath: API_PATH,
      globalOptions: {
        headers: {
          'Content-Type':  'application/json',
          'Accept':  'application/json',
          "access_token_name": localStorage.getItem('accessToken'),
          "client_name": localStorage.getItem('client'),
          "uid_name": localStorage.getItem('uid')
        }
      }
 });

Even after that the response hasn’t changed to the request and I was unable to receive these headers on the server end as well.

However after hours of inspection an idea finally came to me which was to inspect the headers m getting on the server and when I used ruby’s request.header.inspect on my server end application I get the following output with the information required for validation of the token but it seems that the name of the keys of these header values are different form what the devise_token_auth expects to validate token (I went through the source of the devise_auth_token gem here.

"HTTP_ACCESS_TOKEN_NAME"=>"xxxxxxxxxxxxxxxxxx", "HTTP_EXPIRY"=>"xxxxxxxxxxxxxxxxxx", "HTTP_UID"=>"abc@xyz.com", "HTTP_CLIENT_NAME"=>"xxxxxxxxxxxxxxxxxx", "HTTP_TOKEN_TYPE"=>"Bearer"

What I believe is the user is not being set by the devise_token_auth gem based on the headers that are being passed.

After repeatedly going through the documentation of Angular2-token as well as devise_token_auth gem I am confused whether or not to manually add headers for authentication because I believe they are being passed already but with different keys. I would just like to know if that is the case I am experiencing its been almost a full day and I cannot figure out a way to pin point the reason behind the 401 response.

Thanks a lot.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:18 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
VadymBoguslavskycommented, May 16, 2017

I`m not using api gem and still got the same problem. Rails >5

1reaction
neroniakycommented, Feb 7, 2017

Did you check here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there any reason to use HTTP header authorization to send ...
If I use the authorization header, Server will send JWT token through header and client side will store token in local storage.
Read more >
Why You Should Always Use Access Tokens to Secure an API
We explain the difference between access token and ID token and why the latter should never be used to secure an API.
Read more >
Token Authentication in ASP.NET Core 2.0 - A Complete Guide
Token authentication is the process of attaching a token (sometimes called an access token or a bearer token) to HTTP requests in order...
Read more >
How to Build an Authentication API with JWT Token in Node.js
This tutorial will provide a step by step guide on how to create an authentication API with JWT in Node.js.
Read more >
API Keys vs OAuth Tokens vs JSON Web Tokens - Zapier
For an API to be a powerful extension of a product, it almost certainly needs authentication. By building API calls that can read,...
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