-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Access to current_user variable? #28
Comments
@kareemgrant - |
I've run into this a handful of times recently with plain, old devise; whatever mechanism/pattern devise uses in recent versions to include the helpers app-wide seems to need
|
@kareemgrant - Can you verify that your controllers include the controller concern describe here? |
@lynndylanhurley yep, i've already added the I'm using the Rails-api gem, which excludes view related modules that come with a vanilla rails implementation - maybe this could be the root of the problem? I've inspected the @user/@current_user variable in the source code of the gem here and it successfully returns the logged in user. However, when I try to inspect any of those variables outside of the gem (like in my application controller or interior controllers) those values return nil. @bitzpusher Thanks for the tip, I've tried your advice of adding |
@kareemgrant - I don't think Rails-api is causing this problem. There are no view-related modules involved in setting the Do your controllers all inherit from your class ExampleController < ApplicationController If they don't inherit from class ExampleController < ActionController::API or this: class ExampleController < ActionController::Base If the controllers don't inherit from the |
@lynndylanhurley my base application controller inherits from the ActionController::API (this is where I add the
And all of the controllers inside the api/v1 domain inherit from the above ApplicationController:
|
@kareemgrant - it sounds like everything is set up correctly. You said that you were able to inspect |
@lynndylanhurley yes, I have access to @current_user on this line. I made my ApplicationController inherit from For example - after successfully logging in, I make a request to fetch the user object associated with the id from the ng-token-auth response and then I make a request to fetch the account associated with that user. In both of the those requests, I have access to the current_user and the |
@kareemgrant - this issue has me scratching my head. 1. Client-side environmental issues Are the headers actually sent with each request? Using the network tab in developer tools, inspect each request to the API to ensure that the headers ( If they are not included, make sure that the request that you're making to the API matches the 2. Server-side environmental issues Is the problem that the If the problem is that the
3. Authentication issues Let's try to rule out authentication failure. In DeviseTokenAuth.setup do |config|
config.change_headers_on_each_request = true
end Make sure to restart your server after making this change. If this works, then the problem is related to token handling. Are you making all of the requests from the same browser tab? The auth headers are changed with each request by default. Problems may arise if the auth token isn't kept current. This may happen if requests are made from different tabs within the same browser. |
@lynndylanhurley I went ahead and created a barebones app (using angular with ng-token-auth on the frontend) to determine if the issue was rooted in something quirky with my setup. Using a vanilla rails implementation (no rails-api) I was able to authenticate with the server and get reference the |
@kareemgrant - thanks for the update. Let me know if you ever figure this one out. |
The same thing is happening to me when trying to post a picture using As you can see in my request, the headers are NOT included:
|
@kareemgrant - I pushed an update last night that provides access to @smarquez1 - Is this only when uploading files? |
@lynndylanhurley - Yes, otherwise it works correctly for me. Updated this morning from github. |
@smarquez1 - let's continue this on lynndylanhurley/ng-token-auth#41 |
@kareemgrant did you find what was the issue?? I'm having a similar issue with rails-api |
I had this same issue. Its how it creates the current user. If you are using the namespace V1. it will actually create current_v1_user instead of current_user. (at least with rails 5 beta 3 api option) Same goes for the other functions :authenticate_v1_user! Rails.application.routes.draw do devise_for :users namespace :v1 do mount_devise_token_auth_for 'User', at: 'auth' resources :beer end end Hope that helps. GEMFILE |
How to pass token in header as it is not working in Rails 5 |
Thank you, @aarmel-sw . I thought it was weird that Rails was suggesting I use I didn't think that was supposed to be happening. |
I'm using devise_token_auth along with ng-token-auth library for my angular frontend and everything seems to be working well until I try to access the current_user variable to reference the signed in user. I looked at the code and see that you're using @current_user and that works temporarily, but after a few server requests, I lose reference to @current_user and the variable returns nil even though I have successfully logged in. This prevents me from conducting simple authorization checks (for example: does the current_user belong to the requested account?). Is there something that I'm missing?
In a vanilla devise implementation, you would use a
before_action :authenticate_user!
in your controller to get access to the devise helper methods (current_user, user_signed_in?, etc). However, that is throwing an error as well.Thanks!
The text was updated successfully, but these errors were encountered: