使用 Devise 和(warden)回调进行 Ruby On Rails 身份验证

发布于 2024-12-17 17:04:47 字数 1118 浏览 1 评论 0原文

我有一个使用 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 用户未登录

有人知道发生了什么吗?

我查看了相关问题,但没有帮助:

如何从 Rails 中的 Warden/Devise after_authentication 回调访问会话

warden 回调应放置在 Rails 应用程序中的何处?

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

Where should warden callbacks be placed in a rails app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彩虹直至黑白 2024-12-24 17:04:47

Active Admin 使用的 AdminUser 模型也会执行此回调。所以,也许 if 可以解决你的问题:

 Warden::Manager.after_set_user :scope => :user do |user, auth, opts|
   Cart.find(session[:cart_id]).user = user if user.class == User
 end

The AdminUser model that Active Admin uses also executes this callback. So, maybe, an if can solve your problem:

 Warden::Manager.after_set_user :scope => :user do |user, auth, opts|
   Cart.find(session[:cart_id]).user = user if user.class == User
 end
ま柒月 2024-12-24 17:04:47

事实上,事实证明,这个问题是通过在设计初始化文件中设置 Warden 中的默认范围来解决的。

  # Configure the default scope given to Warden. By default it's the first
  # devise role declared in your routes (usually :user).
  # config.default_scope = :user

由于活动管理路由已添加到我的用户的设计路由之上,因此 adminuser 成为默认用户。

Actually it turned out the issue was solved by setting the default scope in warden,in the devise initializer file.

  # Configure the default scope given to Warden. By default it's the first
  # devise role declared in your routes (usually :user).
  # config.default_scope = :user

Since the active admin routes were added above the devise routes for my user, the adminuser became the default user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文