简体   繁体   中英

devise rails current_user vs user_signed_in?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM