0

I know there is a wiki page about it, but since I'm very new to Rails I am having lot of difficulties in understanding that page.

I had to override registration controller for my user. When user fails to signin I want him to be redirected to my custom signin page. But the application send him to the signin page inside the gem.

How can I accomplish that? Where should I put this class and how can I change it? I have multiple models, each of them has a different signin page. How can I set the scope for each model?

class CustomFailure < Devise::FailureApp

  def redirect_url

    #return super unless [:worker, :employer, :user].include?(scope) #make it specific to a scope
    new_user_session_url(:subdomain => 'secure')
  end

 # You need to override respond to eliminate recall
 def respond
   if http_auth?
     http_auth
   else
     redirect
   end
 end

end

Devise is a powerful gem but some wiki pages don't consider that there can be lots of new programmers

Barbared
  • 800
  • 1
  • 8
  • 25
  • 1
    [What have you tried?](http://whathaveyoutried.com) – sczizzo Jun 13 '12 at 15:35
  • Take a look at this [post][1]. It may help you. [1]: http://stackoverflow.com/questions/5832631/devise-redirect-after-login-fail – Kleber S. Jun 13 '12 at 15:36
  • Ok this is what I tried: I've already set in routes for each of my model the new_"model"_session_path as the wiki suggests but I have the same problem of the question up here. Fails signin do not redirect to my views but to devise ones – Barbared Jun 13 '12 at 15:47
  • where did you place your CustomFailure.rb file? Does it still look the same as you posted above? – ply Jun 13 '12 at 16:03
  • @ply the problem is this. I don't know how to put that class. Should I create a new file for each of model and where should I put that file? Or do I have to keep just one file for all of models? – Barbared Jun 13 '12 at 16:10
  • Don't forget to restart the webserver. – Kleber S. Jun 13 '12 at 16:44

2 Answers2

1

I have my auth failure over-ride class in /lib directly.

Here's a bare-bones version that shows how to handle different scopes for users.

class MyAuthFailure < Devise::FailureApp

  # add different cases here for diff scopes.
  def redirect_url 
    if warden_options[:scope] == :user 
      root_path 
    elsif warden_options[:scope] == :admin 
      admin_root_path 
    end 
  end 

  # You need to override respond to eliminate recall
  def respond
    if http_auth?
      http_auth
    else
      redirect
    end
  end  
end

You'd put this class in /lib/my_auth_failure.rb

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
  • Nope. It doesn't work for me. I did like you said.I need to register admin too. `elsif warden_options[:scope] == :admin new_admin_session_path end` And from my routes: `new_admin_session GET /admin(.:format) admin/sessions#new` but it keeps redirecting to /signin instead of /admin – Barbared Jun 14 '12 at 10:54
0

This solution works great, but the you'll run into problems testing if you don't do what is suggested in this article:

https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated

Your tests will still redirect using the default.

for example:

current_path.should == signin_path

would fail because the current path would actually be users/sign_in or something.

jacklin
  • 2,739
  • 1
  • 24
  • 31