Rails & Devise: Override SessionsController

前端 未结 3 431
臣服心动
臣服心动 2021-02-01 23:22

I\'m trying to set up a sign in form on my home page. I managed to do it by following the Wiki

Except, if the login infos are incorrect, the /devise/session/new.ht

3条回答
  •  难免孤独
    2021-02-02 00:16

    You have to override your custom failure devise Class.

    Add this custom failure class under lib/custom_failure.rb

    class CustomFailure < Devise::FailureApp
      def respond
        if http_auth?
          http_auth
        elsif warden_options[:recall]
            recall
        else
          redirect
        end
      end
    
      def redirect
          store_location!
          flash[:alert] = i18n_message unless flash[:notice]
          redirect_to "/"
      end
    
      def recall
        env["PATH_INFO"] = attempted_path
        flash.now[:alert] = i18n_message(:invalid)
        self.response = recall_controller.action(warden_options[:recall]).call(env)
      end
    
      protected
    
      def i18n_message(default = nil)
        message = warden.message || warden_options[:message] || default || :unauthenticated
    
        if message.is_a?(Symbol)
          I18n.t(:"#{scope}.#{message}", :resource_name => scope,
                 :scope => "devise.failure", :default => [message, message.to_s])
        else
          message.to_s
        end
      end
    
      def warden_options
        env['warden.options']
      end
    
      def warden
        env['warden']
      end
    
      def recall_controller
        "#{params[:controller].camelize}Controller".constantize
      end
    
    
      def attempted_path
        warden_options[:attempted_path]
      end
    
      def store_location!
        session[:"#{scope}_return_to"] = attempted_path if request.get? && !http_auth?
      end
    
      def attempted_path
        warden_options[:attempted_path]
      end
    
      def http_auth?
        !Devise.navigational_formats.include?(request_format) || (request.xhr? && Devise.http_authenticatable_on_xhr)
      end
    end
    

    Write this under config/application.rb

    config.autoload_paths += %W(#{config.root}/lib)
    

    _________________Edit __________________

    You said when the 'sign up goes wrong', this means the control should be in registrations_controller create method. Something must be wrong there, I guess. Try to add these routes.

    devise_for :users
    
      devise_scope :user do
        root :to => "devise/sessions#new"
        get "sign_in", :to => "devise/sessions#new"
        get "sign_out", :to => "devise/sessions#destroy"
        get "sign_up", :to => "devise/registrations#new"
      end
    

    ________________Edit 2 ________________

    Add this in views/devise/registrations/new.html.erb

    <%= devise_error_messages! %>
    

提交回复
热议问题