How to access session from Warden/Devise after_authentication callback in Rails

怎甘沉沦 提交于 2019-11-30 19:04:54
polmiro

"auth.session" get/sets the data in the session key "warden.user.#{scope}.session".

Supposing you had saved pending_ids within your rails app:

session[:pending_ids] = ...

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.

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

You can access the session store via auth:

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

You can also access the session through auth.request.session.

So your example would be:

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

you can also find the whole session from the auth.raw_session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!