我在配置中尝试了这个:
### 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 config中的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是零。
用于REDIRECT的控制器逻辑
来自Guillermo Siliceo Trueba的解决方案不适用于你的Users::SessionsController#create
动作,因为你没有通过关键字the parent class create
action调用super
。
来自类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
中使用此解决方案来处理html
和json
请求,您可以实现相同的。
如果控制器接收到格式为request
的.json
,则执行format.json do .. end
之间的代码,如果request
到达format.html
,则调用父方法(我覆盖所有场景)。
Rails路由器通过在网址末尾附加的.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
请求将被路由to the super
create action。您只需要将父方法def after_sign_in_path_for(resource)
覆盖为I am doing in my ApplicationController
。
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
Component渲染它们
令牌认证
user.generate_auth_token
user.save
我使用simple_token_authentication
for devise,它也允许你regenerate the token every time the user signs in。
你只需安装gem并添加acts_as_token_authenticatable
inside your user
model。
class User < ApplicationRecord
acts_as_token_authenticable
end
您将标题X-User-Email
和X-User-Token
与相应的值一起传递,如in their guide所述。
您可能需要在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