Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find a devise user by it's session id?

Given session["session_id"] is it possible to find the logged in User to which that session belongs to?

like image 201
Geo Avatar asked Apr 26 '12 17:04

Geo


People also ask

How do I get a current user in devise?

1 Answer. Show activity on this post. If user is not logged in, i.e., current_user=nil then user would be redirected to login page with a flash message.

How does devise session work?

Devise stores some information in the database ( last_sign_in_at , last_sign_in_ip , etc.), but relies on cookies to simulate stateful session consistency overtime. The cookie has a "TTL" or time to live, and that cookie is written to the browser when "remember me" is checked.


1 Answers

At least on my system (rails 3.2, devise 2.0.4), you can do it like this:

session is:

{"session_id"=>"be02f27d504672bab3408a0ccf5c1db5", "_csrf_token"=>"DKaCNX3/DMloaCHbVSNq33NJjYIg51X0z/p2T1VRzfY=", "warden.user.user.key"=>["User", [3], "$2a$10$5HFWNuz5p6fT3Z4ZvJfQq."]}

session["warden.user.user.key"][1][0], then is 3.

So, I'd find it as:

User.find(session["warden.user.user.key"][1][0])
like image 71
Kevin Bedell Avatar answered Oct 22 '22 03:10

Kevin Bedell