Class: Umbreo::Models::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/umbreo/models/authentication.rb

Instance Method Summary collapse

Constructor Details

#initialize(email = nil, password = nil) ⇒ Authentication

Init data



8
9
10
11
12
13
14
15
16
# File 'lib/umbreo/models/authentication.rb', line 8

def initialize(email = nil, password = nil)
  config    = Configuration.new

  @email    = email
  @password = password
  @endpoint = (config.read_file_authentication[:end_point] rescue nil) || SERVER_END_POINT
    @message  = Helpers::AlertMessage
  @errors   = []
end

Instance Method Details

#errorObject

return error from validation



33
34
35
# File 'lib/umbreo/models/authentication.rb', line 33

def error
  @errors.first
end

#retrieve_credentails!Object

check current_user



86
87
88
89
90
91
92
93
94
95
# File 'lib/umbreo/models/authentication.rb', line 86

def retrieve_credentails!
  config = Configuration.new

  if config.is_valid_user?
    config.read_file_authentication
  else
    @message.show_error_message("Invalid credentials. Please try to login `umbreo login` again.")
    exit!(0)
  end
end

#sign_in_and_retrieve_token!Object

Access server and get access token



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/umbreo/models/authentication.rb', line 40

def 
  begin
    _request =  Typhoeus.post(
                  "#{@endpoint}/api/v1/users/retrieve_token",
                  body: {
                    email: @email,
                    password: @password,
                  }
                )

    @data = JSON.parse _request.response_body
  rescue Exception => e
    @data = { message: "An error of type #{e.class} happened, #{e.message}"}
  end
end

#sign_in_callbackObject

Action after retrive token



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/umbreo/models/authentication.rb', line 68

def 
  if @data['success']
    @message.show_success_message("Setting up configuration file at ~/.umbreoconfig...")

    config        = Configuration.new
    end_point_url = config.read_file_authentication[:end_point] rescue nil
    config.write_file_authentication(@data, end_point_url)
    
    sleep 1
    puts "Logged in as #{ Rainbow(@data['email']).cyan }"
  else
    @message.show_error_message(@data['message'])
  end
end

#sign_outObject

remove file configuration



59
60
61
62
63
# File 'lib/umbreo/models/authentication.rb', line 59

def sign_out
  config = Configuration.new
  config.delete
  puts 'Local credentials cleared.'
end

#valid?Boolean

Check credentails validation

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
# File 'lib/umbreo/models/authentication.rb', line 21

def valid?
  @errors << "Email or Password Can not be blank!" if @email.blank? || @password.blank?
  if @email.present?
    @errors << "Invalid format email" if @email.match(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i).blank?
  end

  @errors.blank?
end