Redirect user after log in only if it's on root_path

后端 未结 8 2210
傲寒
傲寒 2020-12-09 06:13

I have a root_path on my Rails application that is not user-protected i.e. it\'s a simple portal homepage, with a login form.

After the users log in, I\

相关标签:
8条回答
  • 2020-12-09 06:23

    Just a thought

    You can define two root url one for signed in url which will point to dashboard and second for non signed in users which will point to login page

    define different root url based on some constraints in routes.rb

    root url for signed in users

    constraints(AuthenticatedUser) do 
      root :to => "dashboard"
    end
    

    root url for non signed in users

    root :to=>"users/signin"
    

    then create class AuthenticatedUser in lib/authenticated_user.rb

    class AuthenticatedUser
      def self.matches?(request)
        user_signed_in?
      end
    end
    

    now if user is signed in root_url will point to dashboard else it will point to signin page

    Your can also create two roots using(did not tested it yet)

    root :to => "dashboard", :constraints => {user_signed_in?}
    root :to => "users/signin"
    

    more on constrains http://edgeguides.rubyonrails.org/routing.html#request-based-constraints

    Note

    The priority of url is based upon order of creation, first created -> highest priority resources

    0 讨论(0)
  • 2020-12-09 06:23

    I think you're solution is more complex than necessary. Why don't you just do something simple like this on the action that the login form is posted to:

    def login
      // logic to check whether login credentials authorize user to sign in
      // say 'user_signed_in?' is boolean to determine whether user has successfully signed in
      redirect_to(user_signed_in? ? dashboard_path : root_path)
    end
    
    0 讨论(0)
  • 2020-12-09 06:24

    I'm not sure whether or not you're using an after_filter or before_filter somewhere for your redirects but you might be able to use a skip_filter in your login controller. Then put in your custom redirect as a filter within that controller.

    Skip before_filter in Rails

    0 讨论(0)
  • 2020-12-09 06:28

    It sounds like you're over complicating the issue. If you get into overriding routing variables it just leads to headaches down the line. I would recommend using a before filter to require a login and use the except param or skip that before filter for your landing page if you're using a separate controller. As an example:

    class ApplicationController < ActionController::Base
    
      before_filter :require_login, :except => :root
    
      def root
        # Homepage
      end
    
      protected
    
      def require_login
        redirect_to login_path and return unless logged_in?
      end
    
    end
    

    (Make sure you have logged_in? defined)

    If you are using a separate controller it will look something like this:

    class HomepageController < ApplicationController
    
      skip_before_filter :require_login
      before_filter :route
    
      protected
    
      def route
        redirect_to dashboard_path and return if logged_in?
      end
    
    end
    

    Regarding proper routing after a login, that would come down to what you're doing when you're creating your session. Regardless, this setup should catch anyone that's logged in trying to hit the homepage, and route them to your dashboard and anyone trying to hit restricted content (Anything besides root) and route them to the login_path

    0 讨论(0)
  • 2020-12-09 06:29

    I'm using Omniauth and this method has worked well for me. If you're working with a different strategy, I'm sure you could modify it.

    After they log in, just redirect them to root_path and it will take them to dashboard_path or whatever other default you set in the routes file below.

    Set up your helper and callback methods in the app controller:

    # application_controller.rb
    class ApplicationController < ActionController::Base
      protect_from_forgery
      helper_method :current_user
    
      private
    
      def current_user
        @current_user ||= User.find(session[:user_id]) if session[:user_id]
      end
    
      def authenticate_user!
        unless current_user
          redirect_to root_url
        end
      end
    end
    

    Put the before_filter in the restricted controller to catch unauthorized people:

    # dashboard_controller.rb
    class DashboardController < ActionController::Base
      before_filter :authenticate_user!
    
      def index
      end
    
      # rest of controller here
    
    end
    

    Add these two items to the route. The first one will not be executed if the condition is not met. If it is, then the top one takes precedence and all root_url calls will go to the dashboard

    # routes.rb
    YourAppName::Application.routes.draw do
      root :to => 'dashboard#index', :conditions => lambda{ |req| !req.session["user_id"].blank? }
      root :to => 'static_page#index'
    
      # the rest of your routes
    
    end
    
    0 讨论(0)
  • 2020-12-09 06:30

    You can control this with a before_filter on your application controller.

    0 讨论(0)
提交回复
热议问题