Class: UserAuthenticator

Inherits:
Object
  • Object
show all
Defined in:
app/services/user_authenticator.rb

Instance Method Summary collapse

Constructor Details

#initialize(user, session, authenticator_finder: Users::OmniauthCallbacksController, require_password: true) ⇒ UserAuthenticator

Returns a new instance of UserAuthenticator.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/services/user_authenticator.rb', line 4

def initialize(
  user,
  session,
  authenticator_finder: Users::OmniauthCallbacksController,
  require_password: true
)
  @user = user
  @session = session
  if session&.dig(:authentication) && session[:authentication].is_a?(Hash)
    @auth_result = Auth::Result.from_session_data(session[:authentication], user: user)
  end
  @authenticator_finder = authenticator_finder
  @require_password = require_password
end

Instance Method Details

#authenticated?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'app/services/user_authenticator.rb', line 46

def authenticated?
  return false if !@auth_result
  return false if @auth_result&.email&.downcase != @user.email.downcase
  return false if !@auth_result.email_valid
  true
end

#email_valid?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/services/user_authenticator.rb', line 42

def email_valid?
  @auth_result&.email_valid
end

#finishObject



34
35
36
37
38
39
40
# File 'app/services/user_authenticator.rb', line 34

def finish
  if authenticator
    authenticator.(@user, @auth_result)
    confirm_email
  end
  @session[:authentication] = @auth_result = nil if @session&.dig(:authentication)
end

#has_authenticator?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/services/user_authenticator.rb', line 30

def has_authenticator?
  !!authenticator
end

#startObject



19
20
21
22
23
24
25
26
27
28
# File 'app/services/user_authenticator.rb', line 19

def start
  if authenticated?
    @user.active = true
    @auth_result.apply_user_attributes!
  elsif @require_password
    @user.password_required!
  end

  @user.skip_email_validation = true if @auth_result && @auth_result.skip_email_validation
end