Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise how to redirect to different page (based on some parameter) after sign in?

In my application, I have two different login forms from two controller which will both sign_in through the Devise::SessionsController, the problem is after successful sign in (or failure) I need to redirect to different pages that specific to the controller. How can I do this. I currently have this in my Devise::SessionsController, which

class SessionsController < Devise::SessionsController
    def create
        resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
        return sign_in_and_redirect(resource_name, resource)
      end

      def sign_in_and_redirect(resource_or_scope, resource=nil)
        scope = Devise::Mapping.find_scope!(resource_or_scope)
        resource ||= resource_or_scope
        sign_in(scope, resource) unless warden.user(scope) == resource
        redirect_to dashboard_path
      end

      def failure      
        redirect_to index_path
      end
end
like image 814
Rn2dy Avatar asked Mar 23 '12 07:03

Rn2dy


1 Answers

In application controller

  before_filter :store_location
  def store_location
    unless params[:controller] == "devise/sessions"
      url = #calculate the url here based on a params[:token] which you passed in
      session[:user_return_to] = url
    end
  end

  def stored_location_for(resource_or_scope)
    session[:user_return_to] || super
  end

  def after_sign_in_path_for(resource)
    stored_location_for(resource) || root_path
  end
like image 134
John Plummer Avatar answered Sep 25 '22 20:09

John Plummer