Class: Termworld::Commands::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/termworld/commands/account.rb

Class Method Summary collapse

Class Method Details

.loginObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/termworld/commands/account.rb', line 5

def 
  credential = Credential.new
  return puts Utils::Color.reden "Already logged in" if credential.logged_in?
  print "email: "
  email = $stdin.gets.chomp
  res = $api_client.call(:post, '/token', {email: email})
  return puts Utils::Color.reden "Invalid email" if res.code != 200
  puts Utils::Color.greenen "Sent token to your email, please input below"
  print "token: "
  token = $stdin.gets.chomp
  res = $api_client.call(:post, '/login', {email: email, token: token})
  return puts Utils::Color.reden "Invalid token" if res.code != 200
  File.write(Termworld::CREDENTIAL_FILE_NAME, "#{email}\n#{token}")
  puts Utils::Color.greenen "Login successed!"
end

.logoutObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/termworld/commands/account.rb', line 21

def logout
  credential = Credential.new
  return puts Utils::Color.reden "Not logged in" unless credential.logged_in?
  email, token = Credential.get_credential
  res = $api_client.call(:post, '/logout', {email: email, token: token})
  return puts Utils::Color.reden "Logout failed" if res.code != 200
  daemon = Daemon.new(:logout)
  daemon.stop if daemon.alive?
  File.delete(Termworld::CREDENTIAL_FILE_NAME) if File.exists?(Termworld::CREDENTIAL_FILE_NAME)
  puts Utils::Color.greenen "Logout successed!"
end