Current_user nil after creating a session in ROR using AuthLogic

I'm having issues with AuthLogic and current_user.

I have a Flex4 application using the Cairngorm framework as the front-end, and Ruby On Rails as the external one.

I can log in through the browser and use only ROR. However, when I try to execute it through the Flex4 application, it will work with the error the first time, but work the second time.

What happens is inside user_sessions_controller.rb I have a call

self.current_user.to_xml;

The first time the create action is called, the current_user object returns nil. The second time I call it (without restarting the server or browser), it will be the correct current user.

So this makes me think that current_user is set sometime after the render command inside my create action.

If I need my rails controller to return the current user to the create action, how would I do it?

+4
source share
2 answers

Was there only the same problem ... I don’t know why this is happening, but I tried to call current_user in my create method in my user_sessions_controller.rb , as described below ...

  def create @user_session = UserSession.new(params[:user_session]) if @user_session.save current_user = UserSession.find.user current_user.increment_login_count_for_current_memberships! flash[:notice] = 'Sign in successful.' redirect_to root_path else render action: "new" end end 

Hope this helps!

+2
source

This is because helper methods of current_user are usually defined as before_action. Which means that before the action was completed before you use it during session creation,

I also used this UserSession.find.user, which is great for this utility.

Cheers, Niklas

0
source

Source: https://habr.com/ru/post/1343696/


All Articles