2

What i want is if somebody enter wrong information when signing up or signing in to be redirected to a different page other than default let's say mycontroller#index In my application i'm already overidding Devise::SessionsController and Devise::RegistrationsController.I followed the steps from this link Devise redirect after login fail But still i get redirected to signup page and signin page. I am using Rails 3.2 and Devise 3.4.1 Here is my code....What am i doing wrong or missing?Thank you in advance.

Session controller

class SessionsController < Devise::SessionsController

  def create
    resource = warden.authenticate!(:scope => resource_name, :recall => "#   {controller_path}#failure")
    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
    return render :json => {:success => true}
  end

  def failure
    return render :json => {:success => false, :errors => ["Login failed."]}
  end
end

Registrations controller

class RegistrationsController < Devise::RegistrationsController

  def create
    build_resource

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        return render :json => {:success => true}
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if  is_navigational_format?
        expire_session_data_after_sign_in!
        return render :json => {:success => true}
      end
    else
      clean_up_passwords resource
      return render :json => {:success => false}


      redirect_to {:controller=>"deal",:action=>"confirm_and_pay"}
    end

  end

  def sign_up(resource_name, resource)
    sign_in(resource_name, resource)
  end

end

Routes

devise_for :users, :skip => [:registrations, :sessions]
devise_scope :user do

  get 'signup' => 'devise/registrations#new', :as => :new_user_registration 
  post 'signup' => 'devise/registrations#create', :as => :user_registration 
  get 'users/cancel' => 'devise/registrations#cancel', :as => :cancel_user_registration 
  get 'users/edit' => 'devise/registrations#edit', :as => :edit_user_registration 
  put 'users' => 'devise/registrations#update' 
  delete 'users/cancel' => 'devise/registrations#destroy' ,:as=>:destroy_user_session
  get 'signin' => 'devise/sessions#new', :as => :new_user_session 
  post 'signin' => 'devise/sessions#create', :as => :user_session 
  get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session 
end

Application controller

class ApplicationController < ActionController::Base

  protect_from_forgery

  def after_sign_up_path_for(resource)
    show_deals_path(resource)
  end

  def after_sign_in_path_for(resource)
    sign_in_url = url_for(:action => 'new', :controller => 'sessions', :only_path => false, :protocol => 'http')
    if request.referer == sign_in_url
      super
    else
      stored_location_for(resource) || request.referer || root_path
    end
  end
end

lib/custom_failure.rb

class CustomFailure < Devise::FailureApp
  def redirect_url
    redirect_to {:controller=>"mycontroller",:action=>"index"}
  end

  def respond
    if http_auth?
      http_auth
    else
      redirect
    end
  end
end

application.rb

config.autoload_paths += %W(#{config.root}/lib)
Community
  • 1
  • 1
Katie
  • 27
  • 5
  • First thing is to confirm if your controller is called. `'devise/registrations#new'` calls 'new' method in Devise::SessionsController not your SessionsController. Try replace it as `'registrations#new'`. It should call your controller. – shirakia Dec 16 '14 at 23:41
  • You are right i was not calling my registration controller...let me investigate some more – Katie Dec 16 '14 at 23:58
  • How do i get the session#create to redirect to mycontroller#index after failure...you can put it on answer and i will accept it. – Katie Dec 17 '14 at 00:03

1 Answers1

0

If you want to follow Devise redirect after login fail link, you should add this into config/initializers/devise.rb

config.warden do |manager|
  manager.failure_app = CustomFailure
end
Community
  • 1
  • 1
shirakia
  • 2,369
  • 1
  • 22
  • 33