Skip to content

Instantly share code, notes, and snippets.

@promisepreston
Last active September 8, 2019 06:10
Show Gist options
  • Save promisepreston/3c8832284329a1f3623fabed5233beda to your computer and use it in GitHub Desktop.
Save promisepreston/3c8832284329a1f3623fabed5233beda to your computer and use it in GitHub Desktop.
# app/services/authenticate_user.rb
class AuthenticateUser
def initialize(email, password)
@email = email
@password = password
end
def call
JsonWebToken.encode(user_id: user.id) if user
end
private
attr_accessor :email, :password
def user
user = User.find_by(email: email)
# return user if user && user.authenticate(password)
return user if user&.authenticate(@password)
errors.add(:user_authentication, 'invalid credentials')
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment