forked from nejdetkadir/devise-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticate.rb
28 lines (23 loc) · 842 Bytes
/
authenticate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true
module Devise
module Api
module ResourceOwnerService
class Authenticate < Devise::Api::BaseService
option :params, type: Types::Hash
option :resource_class, type: Types::Class
def call
resource = resource_class.find_for_authentication(params.slice(*resource_class.authentication_keys))
return Failure(error: :invalid_email, record: nil) if resource.blank?
return Failure(error: :invalid_authentication, record: resource) unless authenticate!(resource)
Success(resource)
end
private
def authenticate!(resource)
resource.valid_for_authentication? do
resource.valid_password?(params[:password])
end && resource.active_for_authentication?
end
end
end
end
end