Given session["session_id"]
is it possible to find the logged in User
to which that session belongs to?
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.
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.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With