Class: Usman::AuthenticationService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ AuthenticationService

Returns a new instance of AuthenticationService.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/services/usman/authentication_service.rb', line 6

def initialize(params)
  @login_handle = params[:login_handle]
  @password = params[:password]
  @error = nil

  check_if_user_exists
  if @user
    authenticate
    check_if_user_is_approved
  end

  @user.start_session unless @error
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



4
5
6
# File 'app/services/usman/authentication_service.rb', line 4

def error
  @error
end

#login_handleObject (readonly)

Returns the value of attribute login_handle.



4
5
6
# File 'app/services/usman/authentication_service.rb', line 4

def 
  @login_handle
end

#passwordObject (readonly)

Returns the value of attribute password.



4
5
6
# File 'app/services/usman/authentication_service.rb', line 4

def password
  @password
end

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'app/services/usman/authentication_service.rb', line 4

def user
  @user
end

Instance Method Details

#authenticateObject



37
38
39
# File 'app/services/usman/authentication_service.rb', line 37

def authenticate
  set_error() unless @user.authenticate(@password)
end

#check_if_user_existsObject



28
29
30
31
# File 'app/services/usman/authentication_service.rb', line 28

def check_if_user_exists
  @user = User.where("LOWER(email) = LOWER('#{@login_handle}') OR LOWER(username) = LOWER('#{@login_handle}')").first
  set_error() unless @user
end

#check_if_user_is_approvedObject



33
34
35
# File 'app/services/usman/authentication_service.rb', line 33

def check_if_user_is_approved
  set_error(user_status_error) unless @user.approved?
end

#invalid_login_errorObject



20
21
22
# File 'app/services/usman/authentication_service.rb', line 20

def 
  "authentication.invalid_login"
end

#set_error(id) ⇒ Object



41
42
43
# File 'app/services/usman/authentication_service.rb', line 41

def set_error(id)
  @error ||= id
end

#user_status_errorObject



24
25
26
# File 'app/services/usman/authentication_service.rb', line 24

def user_status_error
  "authentication.user_is_#{@user.status.downcase}"
end