Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 888
Algorini Avatar asked Dec 02 '15 11:12

Algorini


2 Answers

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.

like image 174
DannyRosenblatt Avatar answered Nov 17 '22 22:11

DannyRosenblatt


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

like image 21
Saqib Avatar answered Nov 17 '22 23:11

Saqib