["w-6ren">
gpt4 book ai didi

ruby-on-rails - 分别对用户和管理员进行身份验证

转载 作者:太空宇宙 更新时间:2023-11-03 16:50:34 25 4
gpt4 key购买 nike

class ApplicationController < ActionController::Base

protect_from_forgery

skip_before_filter :authenticate_user! , :only => ["welcome#index"]

# before_filter :authenticate_user! :except => ["welocme#index"]

def after_sign_in_path_for(user)
# user_dashboard_index_path
user_dashboard_index_path
end

def after_sign_out_path_for(user)
welcome_index_path
end

after_filter :authenticate_admin!

def after_sign_in_path_for(admin)
admin_dashboard_index_path
end

def after_sign_out_path_for(admin)
welcome_index_path
end

end

管理员不应访问用户仪表板,同样用户不应访问管理仪表板。

我怎样才能做到这一点?

最佳答案

我在我的项目中做了:

 protect_from_forgery with: :exception

def after_sign_in_path_for(resource)
if user_signed_in?
user_dashboard_index_path
elsif admin_signed_in?
admin_dashboard_index_path
else
xyz_path
end
end

同样适用于注销:

def after_sign_out_path_for(resource)
if user_signed_in?
welcome_index_path
elsif admin_signed_in?
welcome_index_path
else
xyz_path
end
end

用于身份验证:

在(欢迎/索引)

<% if user_signed_in? %>
contant_of user
<% else %>
you are not authenticated #admin can not authenticate this page
<% end %>

希望对您有所帮助

关于ruby-on-rails - 分别对用户和管理员进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23212211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com