开发者

Creating a UserSession In Authlogic On the Fly

开发者 https://www.devze.com 2023-01-03 09:41 出处:网络
We\'re using authlogic, and we want to have a user accounted created and the user l开发者_开发技巧ogged in on the fly when the user visits certain pages. We have a function that we run as the before_f

We're using authlogic, and we want to have a user accounted created and the user l开发者_开发技巧ogged in on the fly when the user visits certain pages. We have a function that we run as the before_filter on the actions where we want the user created on the fly:

   #
   # Require user account, create an anonymous if no account
   #
   def require_user_or_create_anonymous
        unless current_user
          @user = User.new
          @user.name = 'Anonymous'
          @user.anonymous = 1
          @user.crypted_password = ''
          @user.password_salt = ''
          @user.persistence_token = ''
          @user.email = 'email'
          @user.save false
          @user_session = UserSession.new
        end
  end
  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.record
  end

This works to create the user and log the user in. However we're finding that it takes another page load for it to be active. If this is called in the before_filter for a specific action, the current_user method just returns nil in that action. If the page is then reloaded, current_user returns the user object correctly.

Any idea how to fix this?


Figured it out. I needed to update the session info with this:

  @current_user_session = UserSession.find
  @current_user = current_user_session && current_user_session.record

in require_user_or_create_anonymous.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号