badnaam
badnaam

Reputation: 1884

authlogic current_user is nil

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 

Upvotes: 1

Views: 2563

Answers (3)

Bernardo Dias
Bernardo Dias

Reputation: 28

Try to use this to rails2:

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details

  #Authlogic
  filter_parameter_logging :password

  helper_method :current_user

  private

  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
end

and this to rails3:

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details

  helper_method :current_user

  private

  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
end

/config/application.rb

# Configure sensitive parameters which will be filtered from the log file.  
config.filter_parameters += [:password]  

Upvotes: 0

Magesh
Magesh

Reputation: 780

I had similar issue with authlogic and rails 3, and the problem that i figured out was the apache http authentication that was enabled in my production server, here is the solution to get that fixed, check Rails + Authlogic @current_user problem - "Show" method no longer works

Upvotes: 1

Hugo
Hugo

Reputation: 2913

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.

Upvotes: 1

Related Questions