Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define custom Failure for devise in case of two different models User and active admin?

I have two models User and ActiveAdmin on which I want to apply my devise integrations.

I have my custom_failure.rb as follows

class CustomFailure < Devise::FailureApp

  def redirect_url
    login_path
  end

  # def redirect_url
  #   root_path
  # end

  def respond
    if http_auth?
      http_auth
    else
      redirect
    end
  end
end

Which seems to be working great.

Also, can define in my application controller like :

def after_sign_in_path_for(resource)
  # case resource
    if resource.is_a?(Admin)
      admin_dashboard_path
    else
      root_path
    end
end

and

def after_sign_out_path_for(resource_or_scope)
  login_path
end

But the issue is how to use this resource in custom_failure.rb so that I can redirect accordingly to login for user login or for the admin login ?? Acc to current scenario it always redirects to the user login page ??

like image 227
Sahil Grover Avatar asked Feb 12 '13 09:02

Sahil Grover


1 Answers

Try putting custom_failure.rb into you lib folder. Then make sure the file is loaded. Probably you would attempt to load all files in lib automatically.

Then Redirect to a specific page.

UPDATE:

You have to use scope to solve this :-

class CustomFailure < Devise::FailureApp 
  def redirect_url 
    if warden_options[:scope] == :user 
      signin_path 
    else 
      new_admin_user_session_path 
    end 
  end 
  def respond 
    if http_auth? 
      http_auth 
    else 
      redirect 
    end 
  end 
end 
like image 79
My God Avatar answered Sep 28 '22 09:09

My God