class Envirobly::AccessToken

def set

def set
  @token = nil
  while @token.blank?
    begin
      @token = shell.ask("Access Token:", echo: false)
    rescue Interrupt
      shell.say
      shell.say_error "Cancelled"
      exit
    end
    api = Envirobly::Api.new(access_token: self, exit_on_error: false)
    # TODO: Eventually replace with custom `whoami` API that returns name, email...
    if api.list_accounts.success?
      save
      shell.say
      shell.say "Successfully signed in "
      shell.say green_check
    else
      shell.say
      shell.say_error "This token is invalid. Please try again"
      @token = nil
    end
  end
end