繁体   English   中英

Rails Admin未通过Cancancan或Devise进行身份验证

[英]Rails Admin not authenticating with Cancancan or Devise

我在配置中尝试了这个:

  ### Popular gems integration
  config.authenticate_with do
    warden.authenticate! scope: :user
  end
  config.current_user_method(&:current_user)

在这里,访问/admin会使我陷入登录循环中。 我登录然后重定向到登录页面。

以前我尝试过

class Ability
  include CanCan::Ability

  def initialize(user)
    # Define abilities for the passed in user here. For example:
    #
      user ||= User.new # guest user (not logged in)
      if user.admin?

在rails admin配置中使用CanCanCan身份验证。 这导致用户始终为零。 即使我放进去

config.current_user_method(&:current_user)

如何解决此问题以验证是否是管理员?

编辑:在会话控制器中

      if user.admin
        redirect_to rails_admin_url
      else
        render json: user
      end

而我陷入了重新登录的重定向中。

路线:

Rails.application.routes.draw do
  mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
  devise_for :users, controllers: { sessions: :sessions },
                      path_names: { sign_in: :login }
... then many resources lines

编辑:

会话控制器:

class SessionsController < Devise::SessionsController
  protect_from_forgery with: :null_session

  def create
    user = User.find_by_email(sign_in_params[:email])
    puts sign_in_params
    if user && user.valid_password?(sign_in_params[:password])
      user.generate_auth_token
      user.save
      if user.admin
        redirect_to rails_admin_url
      else
        render json: user
      end
    else
      render json: { errors: { 'email or password' => ['is invalid'] } }, status: :unprocessable_entity
    end
  end
end

Rails管理员配置,请尝试以下操作:

  ### Popular gems integration
  config.authenticate_with do
    redirect_to merchants_path unless current_user.admin?
  end
  config.current_user_method(&:current_user)

在ApplicationController中:

def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
  helper_method :current_user

试过这个,current_user为nil。

您可能需要在ApplicationController上定义after_sign_in_path_for方法

默认情况下,它首先尝试在会话中找到有效的resource_return_to密钥,然后回退到resource_root_path,否则它将使用根路径。 对于用户范围,可以通过以下方式定义默认url:

get '/users' => 'users#index', as: :user_root # creates user_root_path

namespace :user do
  root 'users#index' # creates user_root_path
end

如果未定义资源根路径,则使用root_path。 但是,如果此默认值还不够,则可以对其进行自定义,例如:

def after_sign_in_path_for(resource)
  stored_location_for(resource) ||
    if resource.is_a?(User) && resource.can_publish?
      publisher_url
    else
      super
    end
end

重定向控制器逻辑

Guillermo Siliceo Trueba的解决方案不适用于您的Users::SessionsController#create操作,因为您没有通过关键字super调用父类create操作

该方法create从类Devise::SessionsController运行respond_with使用在上线location ,从返回的值after_sign_in_path_for(resource)

class Devise::SessionsController < DeviseController
  # POST /resource/sign_in
  def create
    self.resource = warden.authenticate!(auth_options)
    set_flash_message!(:notice, :signed_in)
    sign_in(resource_name, resource)
    yield resource if block_given?
    respond_with resource, location: after_sign_in_path_for(resource)
  end
end

我在我的Users::SessionsController使用此解决方案来处理htmljson请求,您可以实现相同的解决方案。

如果控制器接收到该request与格式.json ,之间的代码format.json do .. end被执行时,如果request与到达format.html父方法被称为(I涵盖所有的场景)。

Rails路由器从URL末尾附加的.json.html识别请求格式(例如GET https://localhost:3000/users.json

class Users::SessionsController < Devise::SessionsController
  def create
    respond_to do |format|
      format.json do 
        self.resource = warden.authenticate!(scope: resource_name, recall: "#{controller_path}#new")
        render status: 200, json: resource
      end
      format.html do 
        super
      end
    end
  end
end

html请求将被路由super创建动作 您只需要像我在ApplicationController所做的那样,从父类重写方法def after_sign_in_path_for(resource)

if resource.admin则只需从此方法return rails_admin_url并跳过其他行,否则按照正常行为并调用super#after_sign_in_path_for(resource)

def after_sign_in_path_for(resource)
  return rails_admin_url if resource.admin
  super
end

认证错误信息

warned.authenticate! 将保存在self.resource.errors内部的错误消息。 您只需要使用json_response[:error]在设备上显示错误并在前端中处理响应即可。

我在SignInScreen组件中渲染它们

在此处输入图片说明

令牌认证

user.generate_auth_token
user.save

我使用simple_token_authentication进行设计 ,这也使您每次用户登录时都可以重新生成令牌

您只需安装gem并user模型中添加acts_as_token_authenticatable

class User < ApplicationRecord
   acts_as_token_authenticable
end

您按照标头指南中的说明传递标头X-User-EmailX-User-Token

simple_token_authentication其他替代方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM