12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/pcli/services/authenticate.rb', line 12
def run(should_prompt = true)
if should_prompt
output.puts Pl.dim('Please login.')
output.puts
end
username = prompt.ask('Username:')
password = prompt.mask('Password:')
totp = prompt.ask('TOTP Code:')
output.puts
spinner = SimpleSpinnerBar.start('Authenticating...', output)
response = api.auth(username, password, totp)
if response.success?
api.token = response.json['token']
expires_at = DateTime.parse(response.json['expiresAt'])
time = distance_of_time_in_words(DateTime.now, expires_at)
spinner.success("#{Pl.green('Authenticated')} for the next #{Pl.yellow(time)}")
true
else
spinner.failure
output.puts
if response.known_error?
if response.error.type == 'invalid_credentials'
output.puts Pl.yellow('Invalid credentials!')
output.puts
return run(false)
end
puts 'Server:'
Output::Padded.show([
"#{Pl.yellow(response.error.title)} #{Pl.dim("(#{response.error.status})")}",
'',
response.error.message
], output, screen)
else
output.puts "Server #{Pl.yellow("(#{response.code})")}:"
output.puts Output::Padded.show(response.body, output, screen)
end
false
end
end
|