micro-blogger/app/commands/authenticate_user.rb

24 lines
481 B
Ruby
Raw Normal View History

2018-11-10 18:46:47 -06:00
# frozen_string_literal: true
class AuthenticateUser < Imperator::Command
include ActiveModel::Validations
string :email
string :password
validates :email, presence: true
validates :password, presence: true
def action
JsonWebToken.encode(user_id: user.id) if user
end
def user
user = @user ||= User.find_by(email: @email)
return user if user&.authenticate(@password)
errors.add :user_authentication, 'invalid credentials'
nil
end
end