Class: Searls::Auth::AuthenticatesUser

Inherits:
Object
  • Object
show all
Defined in:
lib/searls/auth/authenticates_user.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initializeAuthenticatesUser

Returns a new instance of AuthenticatesUser.



4
5
6
# File 'lib/searls/auth/authenticates_user.rb', line 4

def initialize
  @parses_time_safely = ParsesTimeSafely.new
end

Instance Method Details

#authenticate_by_email_otp(email_otp, session) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/searls/auth/authenticates_user.rb', line 9

def authenticate_by_email_otp(email_otp, session)
  if session[:searls_auth_email_otp_verification_attempts] > Searls::Auth.config.max_allowed_email_otp_attempts
    return Result.new(success?: false, exceeded_email_otp_attempt_limit?: true)
  end

  generated_at_value = session[:searls_auth_email_otp_generated_at]
  if generated_at_value.present? &&
      (generated_at = parse_otp_timestamp(generated_at_value)) &&
      generated_at > email_otp_expiry_cutoff &&
      email_otp == session[:searls_auth_email_otp] &&
      (user = Searls::Auth.config.user_finder_by_id.call(session[:searls_auth_email_otp_user_id])).present?
    Searls::Auth.config..call(user)
    Result.new(success?: true, user: user)
  else
    Result.new(success?: false)
  end
end

#authenticate_by_password(email, password, session) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/searls/auth/authenticates_user.rb', line 38

def authenticate_by_password(email, password, session)
  user = Searls::Auth.config.user_finder_by_email.call(email)
  return Result.new(success?: false) if user.blank?

  configuration = Searls::Auth.config

  if requires_verification?(configuration) && !configuration.email_verified_predicate.call(user)
    return Result.new(success?: false, email_unverified?: true)
  end

  begin
    ok = configuration.password_verifier.call(user, password)
  rescue NameError
    return Result.new(success?: false) # controller will map to misconfiguration message
  end

  if ok
    configuration..call(user)
    Result.new(success?: true, user: user)
  else
    Result.new(success?: false)
  end
end

#authenticate_by_token(token) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/searls/auth/authenticates_user.rb', line 27

def authenticate_by_token(token)
  user = Searls::Auth.config.user_finder_by_token.call(token)

  if user.present?
    Searls::Auth.config..call(user)
    Result.new(success?: true, user: user)
  else
    Result.new(success?: false)
  end
end