lynndylanhurley / devise_token_auth

Token based authentication for Rails JSON APIs. Designed to work with jToker and ng-token-auth.
Do What The F*ck You Want To Public License
3.52k stars 1.14k forks source link

Token headers are missing when using multiple models and call both current_* #1532

Open sebfie opened 2 years ago

sebfie commented 2 years ago

When posting issues, please include the following information to speed up the troubleshooting process:

Hello, We noted an issue with authentication using the master branch of devise token auth. We noted that response headers didn't return the necessary info, uid and access token.

From our understanding, it's due to the @resource variable being nil when we are in https://github.com/lynndylanhurley/devise_token_auth/blob/master/app/controllers/devise_token_auth/concerns/set_user_by_token.rb#L98

  1. I make a request using Api::Application authenticated headers
  2. I call current_admin => It's ok and @resource is set to the corresponding resource
  3. Then later on the controller I call current_user => @resource is set to nil
  4. Then I call current_admin again => @resource stays to nil
  5. Then because @resource is nil, request headers are not set.

We noted that bug after putting in production an upgrade from ruby 2.7 to ruby 3.1, that included a necessary upgrade of this gem from 1.1.5 to master, the difference of the code between the 2 is here: https://github.com/lynndylanhurley/devise_token_auth/compare/c922580..23d6b81 But we could not pin point the cause of the error.

A DEMO APP pointing the issue is here : https://github.com/sebfie/devise_token_auth_multiple_model_issue

You have a readme to reproduce

sebfie commented 2 years ago

@lynndylanhurley Can you have a look ? Thank you !

jeremylynch commented 2 years ago

Is it possible this may solve your issue?

https://stackoverflow.com/questions/36505937/devise-token-auth-with-multiple-models-and-auth-headers

sebfie commented 1 year ago

We found a monkey solution (do not call current_admin if current_user...) but I think it should be fixed in this gem.

MaicolBen commented 1 year ago

@sebfie can you make a PR?

sebfie commented 1 year ago

Hello, i do not know how to fix it!

sebfie commented 10 months ago

@MaicolBen I think it can be great to fix it. Btw I do not know how, sry