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

4

9

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

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.

Any help would be much appreciated.

Shurlocke answered 6/10, 2011 at 20:6 Comment(0)
E
25

"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).

Energid answered 11/12, 2012 at 0:10 Comment(0)
A
1

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
Askja answered 15/11, 2011 at 11:28 Comment(2)
I found that auth.session did not work. It exists, but is not the same as session from the controller context.Stemware
When i do that i get undefined local variable or method `session' for #<Class:0x007fa2cbfe6c60>Carchemish
C
1

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
Corrinacorrine answered 30/8, 2016 at 3:45 Comment(0)
R
-1

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

Reflexive answered 30/12, 2011 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.