I am using Devise on Rails 4.1 My question is regarding the helpers and how they relate to sessions. current_user : tells you if there is a user session available for the user. user_signed_in: tells you if the user is authenticated.
I cannot understand how a there can be a current_user if the user_signed_in? is false?
What is the difference between the two methods, and how does it relate to sessions.
THanks. Richard Madson
user_signed_in?
is provided as a convenience. You are correct in your assertion that if user_signed_in?
is false
, there will never be a current_user
.
In the devise source code, we can see:
def #{mapping}_signed_in?
!!current_#{mapping}
end
(where user
takes the place of #{mapping}
)
user_signed_in?
simply returns the truthiness of current_user
, ie, false
if current_user
is nil
.
current_user method returns current signed-in user, while user_signed_in? method is used to verify if any user is signed in and returns true or false. if user_signed_in? is false then current_user method will return nil.
https://github.com/plataformatec/devise#controller-filters-and-helpers
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