使用 Devise 和(warden)回调进行 Ruby On Rails 身份验证
我有一个使用 Devise 的 Rails 应用程序,带有用户模型,没有范围。 我还在应用程序中添加了 activeadmin gem,Active Admin 是一个用于在应用程序中添加管理仪表板的 gem。它使用 Devise 来登录用户,并为管理员创建一个单独的 admin_user 模型。
我允许匿名、未登录的用户创建购物车,创建一个会话[:cart_id]
。如果用户登录,我想将用户与购物车关联起来,就像
Cart.find(session[:cart_id]).user = current_user
我计划使用 Wardens 回调 wardens 回调 来实现这一点,如下所示:
Warden::Manager.after_set_user :scope => :user do |user, auth, opts|
Cart.find(session[:cart_id]).user = user
end
但是,如果我这样做,我会收到错误:
<% except user_signed_in? %>
抛出错误 :admin_user 用户未登录
有人知道发生了什么吗?
我查看了相关问题,但没有帮助:
I have a rails app that is using Devise, with a User model, no scope.
I've also added activeadmin gem in to the app, Active Admin is a gem used for adding an admin dashboard in your application. It uses Devise for logging in users and creates a separate admin_user model for the admins.
I am allowing anonymous, non-logged in users to create shopping carts, creating a session[:cart_id]
. If a user logs in I want associate the user with the cart, something like
Cart.find(session[:cart_id]).user = current_user
I was planning to use Wardens callbacks wardens callbacks to impliment this, something like so :
Warden::Manager.after_set_user :scope => :user do |user, auth, opts|
Cart.find(session[:cart_id]).user = user
end
However I get an error if I do that:
<% unless user_signed_in? %>
throws an error :admin_user user is not logged in
Anyone got any ideas what is going on?
I've looked at related questions, but no help:
How to access session from Warden/Devise after_authentication callback in Rails
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Active Admin 使用的
AdminUser
模型也会执行此回调。所以,也许 if 可以解决你的问题:The
AdminUser
model that Active Admin uses also executes this callback. So, maybe, an if can solve your problem:事实上,事实证明,这个问题是通过在设计初始化文件中设置 Warden 中的默认范围来解决的。
由于活动管理路由已添加到我的用户的设计路由之上,因此 adminuser 成为默认用户。
Actually it turned out the issue was solved by setting the default scope in warden,in the devise initializer file.
Since the active admin routes were added above the devise routes for my user, the adminuser became the default user.