Sha256: 0ffa7dd7e77709605fc66e8579896bbf5aca19cdc59674a6f6973f86eeb4f0ef

Contents?: true

Size: 1.76 KB

Versions: 14

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

PUNK::Command.create "login" do
  description "Authenticate a user by email address or mobile number"

  option shortcut: :c, name: :claim, type: String, description: "The user's email or phone"

  def process
    claim = opts[:claim]
    SemanticLogger.flush
    claim ||= ask("Email or Phone: ")
    response =
      PUNK.app.call(
        "REQUEST_METHOD" => "POST",
        "PATH_INFO" => "/sessions",
        "CONTENT_TYPE" => "text/json",
        "SCRIPT_NAME" => "",
        "rack.input" => StringIO.new({ claim: claim }.to_json)
      )
    response = ActiveSupport::JSON.decode(response[-1].first).deep_symbolize_keys
    return if response[:errors].present?
    slug = response[:slug]
    authenticated = false
    while authenticated == false
      SemanticLogger.flush
      secret = ask("Secret: ") { |q| q.echo = '*' }
      response =
        PUNK.app.call(
          "REQUEST_METHOD" => "PATCH",
          "PATH_INFO" => "/sessions/#{slug}",
          "CONTENT_TYPE" => "text/json",
          "SCRIPT_NAME" => "",
          "rack.input" => StringIO.new({ secret: secret }.to_json)
        )
      response = ActiveSupport::JSON.decode(response[-1].first).deep_symbolize_keys
      break if response[:errors].present? && response[:errors].first != 'Secret is incorrect'
      authenticated = response[:message].present?
    end
    response[:message]
  end
end

PUNK::Command.create "logout" do
  description "Delete the current user session"

  def process
    response =
      PUNK.app.call(
        "REQUEST_METHOD" => "DELETE",
        "PATH_INFO" => "/sessions",
        "SCRIPT_NAME" => "",
        "rack.input" => StringIO.new
      )
    response = ActiveSupport::JSON.decode(response[-1].first).deep_symbolize_keys
    response[:message]
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
punk-0.3.6 lib/punk/commands/auth.rb
punk-0.3.5 lib/punk/commands/auth.rb
punk-0.3.4 lib/punk/commands/auth.rb
punk-0.3.3 lib/punk/commands/auth.rb
punk-0.3.2 lib/punk/commands/auth.rb
punk-0.3.1 lib/punk/commands/auth.rb
punk-0.2.0 lib/punk/commands/auth.rb
punk-0.1.4 lib/punk/commands/auth.rb
punk-0.1.3 lib/punk/commands/auth.rb
punk-0.1.2 lib/punk/commands/auth.rb
punk-0.1.0 lib/punk/commands/auth.rb
punk-0.0.3 lib/punk/commands/auth.rb
punk-0.0.2 lib/punk/commands/auth.rb
punk-0.0.1 lib/punk/commands/auth.rb