Rails:before_action:authenticate_admin!不起作用

时间:2022-10-08 23:54:21

I'm doing this in routes.rb:

我在routes.rb中这样做:

devise_for :admins, :skip => [:registrations], controllers: {sessions: 'admins/sessions'}
devise_scope :admin do
  get "admins/home"=> "admins/sessions#home", :as => "admin_home"
end

But when I put the before_action :authenticate_admin! in the sessions controller and open the admins/home the login screen doesn't appear, no errors pop up neither. I get full access to the page without having to log in!

但是当我把before_action:authenticate_admin时!在会话控制器中打开管理员/主页,登录屏幕不会出现,也不会弹出任何错误。我无需登录即可完全访问该页面!

class Admins::SessionsController < Devise::SessionsController
   before_action :configure_sign_in_params, only: [:create]
   before_action :authenticate_admin!

   def home
   end

   def new
     super
   end


   def create
     super
   end


   def destroy
     super
   end

   protected

   def configure_sign_in_params
     devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
   end
end

Any ideas what I might be doing wrong?

我有什么想法可能做错了吗?

1 个解决方案

#1


1  

Your defined route to create session in home action.So change home action

您在家庭行动中创建会话的已定义路线。因此更改主页操作

class Admins::SessionsController < Devise::SessionsController
   before_action :configure_sign_in_params, only: [:create]

   def home
    self.resource = warden.authenticate!(auth_options)
    set_flash_message!(:notice, :signed_in)
    sign_in(resource)
    respond_with resource, location: after_sign_in_path_for(resource)
   end

   def new
     super
   end


   # override method here
  def create
  end


   def destroy
     super
   end

   protected

   def configure_sign_in_params
     devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
   end
end

NOTE: And put this in application controller

注意:并将其放在应用程序控制器中

before_action :authenticate_admin!

#1


1  

Your defined route to create session in home action.So change home action

您在家庭行动中创建会话的已定义路线。因此更改主页操作

class Admins::SessionsController < Devise::SessionsController
   before_action :configure_sign_in_params, only: [:create]

   def home
    self.resource = warden.authenticate!(auth_options)
    set_flash_message!(:notice, :signed_in)
    sign_in(resource)
    respond_with resource, location: after_sign_in_path_for(resource)
   end

   def new
     super
   end


   # override method here
  def create
  end


   def destroy
     super
   end

   protected

   def configure_sign_in_params
     devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
   end
end

NOTE: And put this in application controller

注意:并将其放在应用程序控制器中

before_action :authenticate_admin!