Sha256: 1ccd414d8a1f8cca72f2f9e1cae67aa541db91ecde613e7221ae10533cf68356

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require_relative "../../boilercode"

module Boilercode
  module Commands
    class Login < Boilercode::Command
      def execute(input: $stdin, output: $stdout)
        set_email_and_password
        write_to_config_file
        output.puts pastel.green "Authenticated"
      end

      private

      def set_email_and_password
        if File.exist?(file_path) && credentials[:token]
          @api_key = credentials[:token]
        else
          prompt_for_api_key
        end
      end

      def fetch_bearer_token
        response = HTTParty.post("#{Boilercode::Client.base_uri}/auth", body: payload)
        if response.success?
          @token = JSON.parse(response.body)["token"]
        else
          puts pastel.red "Authentication failed. Please check your credentials."
        end
        @token
      end

      def prompt_for_api_key
        puts pastel.green "Please enter API key:"
        @api_key = $stdin.gets.chomp
      end

      def payload
        {
          user: {email: @email, password: @password}
        }
      end

      def write_to_config_file
        File.write(file_path, {
          token: @api_key
        }.to_yaml)
        @config = Boilercode::Config.new
        puts pastel.green "Updated: #{file_path}"
      end

      def base
        @base ||= Boilercode::Base.new
      end

      def file_path
        config.file_path
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
boilercode-0.1.2 lib/boilercode/commands/login.rb
boilercode-0.1.1 lib/boilercode/commands/login.rb
boilercode-0.1.0 lib/boilercode/commands/login.rb