Reputation: 15863
In the code below, the returned user object is not nil, but not all members have their relevant values e.g.
User id: 18, provider: nil, uid: nil, name: "newonenewone", oauth_token: nil, oauth_expires_at: nil, created_at: "2013-12-28 22:17:35", updated_at: "2013-12-28 22:17:35", email: "[email protected]", encrypted_password: "14972b4..."
But when checking if the user object is nil it returned true! So why this happens and how to fix it.
def self.authenticate(email, submitted_password)
user = find_by_email(email)
return nil if user.nil?
return user if user.has_password?(submitted_password)
end
Upvotes: 1
Views: 505
Reputation: 2068
Please walk through your code in the case when the user is not nil
but the user does not have a password. In that case, the authenticate
method will return nil
. This happens because you are not handling any cases after the second return
statement. This results in a nil
return by Ruby convention.
In other words, you may want to add code below:
def self.authenticate(email, submitted_password)
user = find_by_email(email)
return nil if user.nil?
return user if user.has_password?(submitted_password)
# TODO: handle case when !user.nil? && !user.has_password?
end
Upvotes: 2