8

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

Algorini
  • 824
  • 1
  • 12
  • 19

2 Answers2

10

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.

DannyRosenblatt
  • 895
  • 11
  • 15
8

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

Saqib
  • 685
  • 3
  • 15