Class: ShopifyCLI::Commands::Login

Inherits:
ShopifyCLI::Command show all
Defined in:
lib/shopify_cli/commands/login.rb

Constant Summary collapse

PROTOCOL_REGEX =
/^https?\:\/\//
PERMANENT_DOMAIN_SUFFIX =
/\.myshopify\.(com|io)$/

Instance Attribute Summary

Attributes inherited from ShopifyCLI::Command

#ctx, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ShopifyCLI::Command

call, call_help, check_node_version, check_ruby_version, check_version, #initialize, options, prerequisite_task, recommend_default_node_range, recommend_default_ruby_range, recommend_node, recommend_ruby, run_prerequisites, subcommand, subcommand_registry

Methods included from Feature::Set

#hidden?, #hidden_feature

Constructor Details

This class inherits a constructor from ShopifyCLI::Command

Class Method Details

.helpObject



40
41
42
# File 'lib/shopify_cli/commands/login.rb', line 40

def self.help
  ShopifyCLI::Context.message("core.login.help", ShopifyCLI::TOOL_NAME)
end

.shop_to_permanent_domain(shop) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/shopify_cli/commands/login.rb', line 50

def self.shop_to_permanent_domain(shop)
  url = if PROTOCOL_REGEX =~ shop
    shop
  elsif shop.include?(".")
    "https://#{shop}"
  else
    "https://#{shop}.myshopify.com"
  end

  # Make a request to see if it exists or if we get redirected to the permanent domain one
  uri = URI.parse(url)
  Net::HTTP.start(uri.host, use_ssl: true) do |http|
    response = http.request_head("/admin")
    case response
    when Net::HTTPSuccess, Net::HTTPSeeOther
      uri.host
    when Net::HTTPFound
      domain = URI.parse(response["location"]).host
      if PERMANENT_DOMAIN_SUFFIX =~ domain
        domain
      end
    end
  end
end

.validate_shop(shop, context:) ⇒ Object



44
45
46
47
48
# File 'lib/shopify_cli/commands/login.rb', line 44

def self.validate_shop(shop, context:)
  permanent_domain = shop_to_permanent_domain(shop)
  context.abort(context.message("core.login.invalid_shop", shop)) unless permanent_domain
  permanent_domain
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/shopify_cli/commands/login.rb', line 16

def call(*)
  shop = (options.flags[:shop] || @ctx.getenv("SHOPIFY_SHOP" || nil))
  ShopifyCLI::DB.set(shop: self.class.validate_shop(shop, context: @ctx)) unless shop.nil?

  if shop.nil? && Shopifolk.check
    Shopifolk.reset
    @ctx.puts(@ctx.message("core.tasks.select_org_and_shop.identified_as_shopify"))
    message = @ctx.message("core.tasks.select_org_and_shop.first_party")
    if CLI::UI::Prompt.confirm(message, default: false)
      Shopifolk.act_as_shopify_organization
    end
  end

  password = options.flags[:password] || @ctx.getenv("SHOPIFY_PASSWORD")
  if !password.nil? && !password.empty?
    ShopifyCLI::DB.set(shopify_exchange_token: password)
  else
    IdentityAuth.new(ctx: @ctx).authenticate(spinner: true)
    org = select_organization
    ShopifyCLI::DB.set(organization_id: org["id"].to_i) unless org.nil?
    Whoami.call([], "whoami")
  end
end