-
Notifications
You must be signed in to change notification settings - Fork 187
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
Confused about sending authentication headers for token validation to API. #145
Comments
Hey @91sticker thanks a lot for your detailed description. To answer your question about the headers. Angular2-token includes the following headers on each request, which match the standard configuration for the devise_token_auth gem.
According to your description, this seems to work fine. I think the error you're encountering is connected to the after reload
Step 3 seems to fail. Now we need to figure out why its failing. Could you record the get-request after reload (with for example postman or the chrome developer tools) and post which headers are included? Thanks! |
Thank u so much for the response @neroniaky , using postman I have sent a GET request to the endpoint( The response I downloaded was as follows: Is there any more information that can be of help to pin point the cause, I am happy to provide. Its a bit confusing because it allows me to login and logout but when I try and check the validation it returns 401 status nor its setting any devise helpers on the server site e.g. current_user, user_signed_in? etc. |
Did you check here? |
Yes sir I did in fact, in my set up I had rack-cors integrated on my back end.
I have also tried with setting methods to any as well but that doesn't work as well, occasionally the rails api also throws a cookie overflow exception. And upon a little online research I came to know that can occur because of the cookie information being too big however my auth credentials are residing in the localstorage and not the cookies, could this be the cause? |
@91sticker Have you added the concern in the base controller as noted here? This absence of the concern would explain user not been set by token. |
@geoandri thanks for the response sir, I have that as well in my application_controller.
returning the similar response
Which confuses me whether the server is getting the right headers, in the right format or whether it has something to do with the rack-cors gem's configurations. |
I had a similar problem a while back (didn't happen with Rails 5 though). As far as I can remember it was about the capitalization of the headers. So Rails would show the headers as 'Token-Type', instead of 'token-type', which would be blocked by the CORS-Config (I think ?). So what would be good if you could print out what rails is actually receiving in your controller. Something like this:
|
When I run the puts request.env["access-token"] it returns blank. I find these headers in a long list of headers but with different keys as I have shared above as well.
but 401 when it hits validate_token endpoint. |
@91sticker you are right. If you inspect the env variable it adds "HTTP_" converts minus to underscore and capitalizes all characters. I just tried
and it returns the access token. |
@neroniaky YES!! I can see the access token in the console now. 1- Is it the default behavior or did I do something wrong with the configurations I have tried this on my end with no luck inside devise_token_auth.rb.
Doing this also made it impossible for the user to login successfully. Next I tried making changes to these headers on the client end and inside the init method in my app.component.ts I added these global options but didnt have any luck as well :
In fact I am getting the same old names in the headers with HTTP prefix and all capitalized. Seems like these headers are not being passed or I am not configuring these options right. |
Here is a link to the response to the curl request I sent along with the headers. |
After a lot of googling and going through issues repeatedly, in particular this I realized I had rails-api gem create my application. |
@91sticker thanks a lot for your work. I can't see anything wrong with your implementation either, so upgrading to Rails 5.0 would have been my next suggestion too. At least we now know that Rails 4 with the api gem doesn't work. Gonna put that in the README. |
No problem, Sir! |
I`m not using api gem and still got the same problem. Rails >5 |
@VadymBoguslavsky Did you find a fix? |
Sorry, but no, i've lost that project already.
I have used doorkeeper as i remember it right.
Вт, 27 бер. 2018, 18:26 користувач Gustavo Gabriel <[email protected]>
пише:
… @VadymBoguslavsky <https://github.com/VadymBoguslavsky> Did you find a
fix?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#145 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AV7vrSNhXSN1zrX8lxt49pmwi9CEIWIcks5tiloNgaJpZM4L4Omb>
.
|
Ok, I managed to make it work finally. My problem was that the front-end was not passing the headers to the backend, so I made an interceptor based on this.
|
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.
followed by:
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.
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"=>"[email protected]", "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.
The text was updated successfully, but these errors were encountered: