Sha256: 2c22d19af5f658e3f9ea3063dcb97c88e07c661d2d6dacf3ab92dd97e7f6e704

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Pcli
  module Services
    class Authenticate
      include ActionView::Helpers::DateHelper

      include Depends.on(
        'output', 'prompt', 'api', 'screen'
      )

      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pcli-0.1.1 lib/pcli/services/authenticate.rb
pcli-0.1.0 lib/pcli/services/authenticate.rb