lib/lingohub/commands/auth.rb in lingohub-0.0.4 vs lib/lingohub/commands/auth.rb in lingohub-0.0.5

- old
+ new

@@ -65,20 +65,31 @@ puts "Enter your Lingohub credentials." print "Email: " user = ask - print "Password: " + + print "Password (please leave blank if you want to use your API token): " password = running_on_windows? ? ask_for_password_on_windows : ask_for_password - api_key = Lingohub::Client.auth(:username => user, :password => password, :host => host)['api_key'] + if password.empty? + print "API key: " + api_key = ask + else + api_key = retrieve_api_key(password, user) + end + [user, api_key] end + def retrieve_api_key(password, user) + Lingohub::Client.auth(:username => user, :password => password, :host => host)['api_key'] + end + def ask_for_password_on_windows require "Win32API" - char = nil + char = nil password = '' while char = Win32API.new("crtdll", "_getch", [], "L").Call do break if char == 10 || char == 13 # received carriage return or newline if char == 127 || char == 8 # backspace and delete @@ -106,10 +117,10 @@ write_credentials check rescue ::RestClient::Unauthorized, ::RestClient::ResourceNotFound => e puts "EXCEPTION #{e}" delete_credentials - @client = nil + @client = nil @credentials = nil display "Authentication failed." retry if retry_login? exit 1 rescue Exception => e