如何在 Rails 中从 Warden/Devise after_authentication 回调访问会话 [英] How to access session from Warden/Devise after_authentication callback in Rails
问题描述
我正在尝试从 Rails 3 中 Warden 的 after_authenticate 回调(在 Devise 下运行)访问当前会话.
I'm trying to access the current session from Warden's after_authenticate callback (running underneath Devise) in Rails 3.
在我的应用程序控制器的顶部,我想执行以下操作:
At the top of my application controller I want to do something like:
Warden::Manager.after_authentication do |user,auth,opts|
user.associate_with_ids(session[:pending_ids])
end
最终目标是获取注册前存储在会话中的记录 ID 列表,并在登录后将它们与用户模型相关联.
The ultimate goal is to take a list of record IDs that were stored in the session before sign up and associate them with the user model after sign in.
任何帮助将不胜感激.
推荐答案
"auth.session" 获取/设置会话密钥中的数据 "warden.user.#{scope}.session".
"auth.session" get/sets the data in the session key "warden.user.#{scope}.session".
假设您在 Rails 应用中保存了 pending_ids:
Supposing you had saved pending_ids within your rails app:
session[:pending_ids] = ...
并且您想访问warden hook,您可以通过以下方式访问它:
and you wanted to acces in the warden hook, you could access it this way:
Warden::Manager.after_authentication do |user,auth,opts|
user.associate_with_ids(auth.env['rack.session'][:pending_ids])
end
我花了一段时间才弄明白,所以我想这可能对某人有所帮助.
It took me a while to find that out, so I guess it might be of some help for somebody.
(最初取自 diegoscataglini.com/2012/02/09/383/manipating-sessions-in-wardendevise,现在已经死了).
这篇关于如何在 Rails 中从 Warden/Devise after_authentication 回调访问会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!