We're using authlogic, and we want to have a user accounted created and the user logged 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?