Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding redirect path after user has confirmed account - Devise

What I want to happen is once a user clicks on the confirmation link - and their account is confirmed successfully - they should be redirected to some specified path, if they have a particular role assigned (which is assigned, successfully, in an after_create callback on the User model).

I have created a RegistrationsController:

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    if resource.has_role? :seller
      new_item_path
    else
      root_path
    end
  end
end

But, this always redirects to the root_path....even though, I have verified that, the user does indeed have that role.

Edit 1

It seems that the confirmation request is never sent to the RegistrationsController:

Started GET "/users/confirmation?confirmation_token=KRwZ7MChtxxq4sxxkDLq" for 127.0.0.1 at 2013-05-07 03:52:56 -0500
Processing by Devise::ConfirmationsController#show as HTML
  Parameters: {"confirmation_token"=>"KRwZ7MChtxxq4sxxkDLq"}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'KRwZ7MChtxxq4sxxkDLq' LIMIT 1
   (0.1ms)  BEGIN
   (0.5ms)  UPDATE "users" SET "confirmation_token" = NULL, "confirmed_at" = '2013-05-07 08:52:56.846852', "updated_at" = '2013-05-07 08:52:56.847730' WHERE "users"."id" = 9
   (0.8ms)  COMMIT
   (0.1ms)  BEGIN
   (0.4ms)  UPDATE "users" SET "last_sign_in_at" = '2013-05-07 08:52:56.852250', "current_sign_in_at" = '2013-05-07 08:52:56.852250', "last_sign_in_ip" = '127.0.0.1', "current_sign_in_ip" = '127.0.0.1', "sign_in_count" = 1, "updated_at" = '2013-05-07 08:52:56.853180' WHERE "users"."id" = 9
   (0.7ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 31ms (ActiveRecord: 3.2

Edit 2

This is what my routes.rb looks like

  devise_for :users, :path_names => { :sign_up => "register", 
                                      :sign_in => "login", 
                                      :sign_out => "logout",
                                      :settings => "settings" },
                      :controllers => { :registrations => "registrations" }

  devise_scope :user do
    get "login", :to => "devise/sessions#new"
    get "register", :to => "registrations#new"
        get "settings", :to => "devise/registrations#edit"
    get "logout",   :to => "devise/sessions#destroy"    
  end
like image 795
marcamillion Avatar asked May 07 '13 09:05

marcamillion


1 Answers

I eventually figured it out.

I had to create a ConfirmationsController.rb class that overrides Devise's Confirmation controller.

class ConfirmationsController < Devise::ConfirmationsController
  protected
    def after_confirmation_path_for(resource_name, resource)
      if resource.has_role? :seller
        new_item_path
      else
        root_path
      end
    end
end

Then in my routes.rb:

  devise_for :users, :path_names => { :sign_up => "register", 
                                      :sign_in => "login", 
                                      :sign_out => "logout",
                      :settings => "settings" },
                      :controllers => {:confirmations => "confirmations"}

  devise_scope :user do
    get "login", :to => "devise/sessions#new"
    get "register", :to => "devise/registrations#new"
    get "settings", :to => "devise/registrations#edit"
    get "logout",   :to => "devise/sessions#destroy"    
  end

That seems to work just fine for me.

like image 173
marcamillion Avatar answered Oct 27 '22 00:10

marcamillion