devise rails current_user vs user_signed_in?
Asked Answered
C

2

9

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

Customary answered 2/12, 2015 at 11:54 Comment(0)
G
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

Granvillegranvillebarker answered 2/12, 2015 at 12:13 Comment(0)
F
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.

Fascinator answered 2/12, 2015 at 14:4 Comment(0)
G
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

Granvillegranvillebarker answered 2/12, 2015 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.