Sha256: 85e93c8438a982d13f4450f4041728dbea7ac915b526f0b61c38512f992b36aa

Contents?: true

Size: 1 KB

Versions: 11

Compression:

Stored size: 1 KB

Contents

module Usman
  class AuthenticationService

    attr_reader :login_handle, :password, :error, :user

    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

    def invalid_login_error
      "authentication.invalid_login"
    end

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

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

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

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

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
usman-0.1.5dev4 app/services/usman/authentication_service.rb
usman-0.1.5dev3 app/services/usman/authentication_service.rb
usman-0.1.5dev2 app/services/usman/authentication_service.rb
usman-0.1.5dev1 app/services/usman/authentication_service.rb
usman-0.1.5dev app/services/usman/authentication_service.rb
usman-0.1.5 app/services/usman/authentication_service.rb
usman-0.1.4 app/services/usman/authentication_service.rb
usman-0.1.3 app/services/usman/authentication_service.rb
usman-0.1.2 app/services/usman/authentication_service.rb
usman-0.1.1 app/services/usman/authentication_service.rb
usman-0.1.0 app/services/usman/authentication_service.rb