views:

39

answers:

1

I have the standard current_user methods in my application_controller.

   def current_user_session
        return @current_user_session if defined?
(@current_user_session)
        @current_user_session = UserSession.find
    end

    def current_user
        return @current_user if defined?(@current_user)
        @current_user = current_user_session &&
current_user_session.record
    end

In my UserSessionController create method, I checked for registration_complete? (simply a check on a value of a column, which works fine) and then redirect the user to the user edit page if the registration is not complete. When I debug this, I can see that the attempted_record for the @user_session object exists and it's pointing to the correct user, but the current_user method in the redirect_to edit_user_path(current_user), always returns nil.

What's going wrong here?

def create
        @user_session = UserSession.new(params[:user_session])
        # uses a block to prevent double render error...
        # because oauth and openid use redirects
        @user_session.save do |result|
            if result
                flash[:notice] = "Login successful!"
                if @user_session.new_registration?
                    logger.info "Session controller : new
registration"
                    logger.info "Complete -
#{@user_session.registration_complete?}"
                    flash[:notice] = "Please review profile"
                    redirect_to edit_user_path(current_user)
                else
                    if @user_session.registration_complete?
                        logger.info "Session Controller - registration
complete"
                    else
                        flash[:notice] = "Please complete profile"
                        logger.info "Session Controller - registration
not complete"
                        redirect_to edit_user_path(current_user)
#current_user nil here
                    end
                end
                redirect_to current_user ? profile_url(current_user) :
login_url
            else
                if @user_session.errors.on(:user)
                    # if we set error on the base object, likely it's
because we didn't find a user
                    render :action => :confirm
                else
                    render :action => :new
                end
            end
        end
    end 
A: 

I have seen this and used that very same authentication system on one of my applications. You don't mention it and that's why I'm proposing this to you if you do have then I'd need to see more of your application_controller, so anyway try and add this before the methods.

helper_method :current_user

I just had a duh moment, try doing this instead of passing a block to the save method.

if @user_session.save
  # rest of your logic here
end

"if result" might not do what you expect it to do.

Else inspect this @user_session.record instead of current_user, if it's still nul your problem is not current_user.

I need to get home, I'll check back later tonight.

Hugo
Thanks for the reply. Here is my appcontroller. I already have that helper method.http://pastie.org/1113118
badnaam
Ok, looks alright to me, can you tell me if current_user under the @user_session.new_registration? validation is non nul ? I'm trying to find fault with anything and I can't, I'm thinking @user_session.registration_complete? might change what's inside @user_session. Otherwise store current_user inside another variable such as @user then use that in your redirect to see if it works.
Hugo
Actually is current_user working anywhere in that create method ?
Hugo
actually, no it isnt.I have updated the pastie with the session.rb file of authlogic-connect, which is the gem I am using for aeuthetication. It contains the registration complete method.
badnaam
Edited original response
Hugo
Thanks Hugo. authlogic-connect requires passing a block for oauth authentication, since it's a two step process.
badnaam
Well I picked up the example at http://github.com/viatropos/authlogic-connect-example.git and thought it would be easy to get up and running with openid at least (as I have yet to do anything with oauth). If you started from that example I think there are some serious issues with it, one being that there is no such thing as params[:user_session] passed from the form. I leave with this, if you only need openid authentication with authlogic check out this railscast http://railscasts.com/episodes/170-openid-with-authlogic. Sorry and goodluck.
Hugo