Save 36% for Black Friday! Learn more

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Ruby Devise: Inconsistency in current_user in API controller VS. Pre-existing controller

Pankaj Nagla asked in General

Hello Everyone,

I have started Rails Training Courses, But I can't able to solve this code.

I'm attempting to access current_user from a custom API controller as shown below:


class Api::V1::UserProfileController < ApplicationController
def index
puts current_user.inspect
end
private
def set_default_format
request.format = :json
end
end


To give a bit of context. I'm integrating React into an already existing project. I'm attempting to get the current user that is logged in. But this always returns as nil and when user_signed_in? the method is called, it's always false. However, I'm able to get current_user from the Users Controller. So I feel as though there is an inconsistency in the session. How can I access current_user from the API controller that reflects the state on other controllers?

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.