John
John

Reputation: 6622

too many redirects while creating a new session

a user of my website must first login to see every page of it. My current solution results in a "too many redirects" error message in the browser, I suspect something goes wrong so it endlessly goes back and forth, but where?

In the application controller I have this:

before_filter :authenticate_user

def authenticate_user
  unless session[:user_id]
    flash[:alert] = I18n.t :session_unauthenticated
    redirect_to login_path
  end
end

The "login_path" goes to "sessions/new", which looks like this:

    def new
      user = User.find_by_email(params[:email])
      if user && user.authenticate(params[:password])
        session[:user_id] = user.id
        redirect_to root_url, :notice => I18n.t(:session_logged_in)
      else
        flash.now.alert = I18n.t(:session_invalid)
        render "new"
      end  
    end

Thanks for your help!

Upvotes: 1

Views: 3766

Answers (1)

tbuehlmann
tbuehlmann

Reputation: 9110

Your before_filter is active for each and every action (since it's in the ApplicationController) and therefore also for your sessions#new action. Meaning you cannot even enter the login action. I suggest you put the before_filter in every needed controller and specify the actions with before_filter :authenticate_user, :except => [:new, :create, :destroy] or whatever you need.

Upvotes: 2

Related Questions