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

Instance Method Details

#authenticate_by_short_code(short_code, session) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/searls/auth/authenticates_user.rb', line 6

def authenticate_by_short_code(short_code, session)
  if session[:searls_auth_short_code_verification_attempts] > Searls::Auth.config.max_allowed_short_code_attempts
    return Result.new(success?: false, exceeded_short_code_attempt_limit?: true)
  end

  if session[:searls_auth_short_code_generated_at].present? &&
      Time.zone.parse(session[:searls_auth_short_code_generated_at]) > Searls::Auth.config.token_expiry_minutes.minutes.ago &&
      short_code == session[:searls_auth_short_code] &&
      (user = Searls::Auth.config.user_finder_by_id.call(session[:searls_auth_short_code_user_id])).present?
    Searls::Auth.config.&.call(user)
    Result.new(success?: true, user: user)
  else
    Result.new(success?: false)
  end
end

#authenticate_by_token(token) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/searls/auth/authenticates_user.rb', line 22

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