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.

Alex Dunae
  • 1,290
  • 3
  • 17
  • 28

4 Answers4

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

Matt
  • 74,352
  • 26
  • 153
  • 180
polmiro
  • 1,966
  • 15
  • 22
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
sequielo
  • 1,541
  • 1
  • 18
  • 27
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
fabi
  • 2,978
  • 1
  • 19
  • 23
-1

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

leomayleomay
  • 553
  • 1
  • 7
  • 16