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

2023-11-23

我正在尝试从 Warden 访问当前会话验证后Rails 3 中的回调(在 Devise 下运行)。

在我的应用程序控制器的顶部我想做一些类似的事情:

Warden::Manager.after_authentication do |user,auth,opts|
  user.associate_with_ids(session[:pending_ids])
end

最终目标是获取注册前存储在会话中的记录 ID 列表,并在登录后将它们与用户模型关联起来。

任何帮助将非常感激。


"授权会话“获取/设置会话密钥中的数据”Warden.user.#{scope}.session".

假设您已在 Rails 应用程序中保存了ending_ids:

session[:pending_ids] = ...

如果你想在 Warden hook 中访问,你可以这样访问它:

Warden::Manager.after_authentication do |user,auth,opts|
  user.associate_with_ids(auth.env['rack.session'][:pending_ids])
end

我花了一段时间才发现这一点,所以我想这可能对某人有帮助。

(originally taken from diegoscataglini.com/2012/02/09/383/manipulating-sessions-in-wardendevise, which is now dead).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐