Class: Belt::CLI::AuthCommand

Inherits:
Object
  • Object
show all
Includes:
AppDetection
Defined in:
lib/belt/cli/auth_command.rb

Constant Summary collapse

TEMPLATE_DIR =
File.expand_path('../../templates/generate/auth', __dir__)
MODULE_DIR =
'infrastructure/modules/app'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AppDetection

#detect_app_name, #detect_environments, #detect_namespace, #find_contracts_file_path, #find_routes_file_path, #find_schema_file_path, #s3_safe_name

Constructor Details

#initialize(pools:, force: false, signup: false) ⇒ AuthCommand

Returns a new instance of AuthCommand.



89
90
91
92
93
94
95
# File 'lib/belt/cli/auth_command.rb', line 89

def initialize(pools:, force: false, signup: false)
  @pool_names = pools
  @force = force
  @signup = 
  @app_name = detect_app_name
  @pools = 
end

Class Method Details

.destroy(_args) ⇒ Object



28
29
30
# File 'lib/belt/cli/auth_command.rb', line 28

def self.destroy(_args)
  new(pools: [], force: false).remove
end

.parse_pools(args) ⇒ Object

Parse pool names from args. Default to ["main"] if none provided.



83
84
85
86
87
# File 'lib/belt/cli/auth_command.rb', line 83

def self.parse_pools(args)
  names = args.reject { |a| a.start_with?('-') }
  names = ['main'] if names.empty?
  names.map(&:downcase).map { |n| n.gsub(/[^a-z0-9_]/, '_') }
end


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/belt/cli/auth_command.rb', line 32

def self.print_help
  puts <<~HELP
    Generate Cognito user pool infrastructure for authentication.

    Usage: belt generate auth [pool_names...] [options]

    Options:
      --signup        Allow public user registration (generates frontend views)
      --force, -f     Overwrite existing cognito.tf (skip collision check)

    Arguments:
      pool_names      Optional pool names for multiple user pools.
                      If omitted, generates a single pool named "main".

    Examples:
      belt g auth                        # Admin-only, single pool
      belt g auth --signup               # Public signup with frontend views
      belt g auth web                    # Named pool: "web"
      belt g auth web mobile             # Two pools
      belt g auth --force                # Overwrite existing

    What this generates:
      infrastructure/modules/app/cognito.tf          User pool + client resources
      infrastructure/modules/app/cognito_outputs.tf  Pool ID, ARN, and client ID outputs

    With --signup (when frontend/ exists):
      frontend/src/lib/auth.js                       Auth module (signIn, signUp, etc.)
      frontend/src/lib/apiClient.js                  API client with Authorization header
      frontend/src/pages/auth/Login.jsx              Login page
      frontend/src/pages/auth/SignUp.jsx             Registration page
      frontend/src/pages/auth/ConfirmEmail.jsx       Email verification page
      frontend/src/components/ProtectedRoute.jsx     Route guard component

    Without --signup (admin-only, default):
      frontend/src/lib/auth.js                       Auth module (signIn only)
      frontend/src/lib/apiClient.js                  API client with Authorization header
      frontend/src/pages/auth/Login.jsx              Login page
      frontend/src/components/ProtectedRoute.jsx     Route guard component

    It also patches:
      infrastructure/modules/app/main.tf             Adds cognito_user_pool_arns to conveyor_belt

    After running:
      1. Review the generated Cognito config in cognito.tf
      2. Add auth: :cognito to your routes namespace
      3. Run `belt deploy` to create the user pool
      4. Create your account (admin-only): aws cognito-idp admin-create-user ...
  HELP
end

.run(args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/belt/cli/auth_command.rb', line 15

def self.run(args)
  if args.include?('--help') || args.include?('-h')
    print_help
    exit 0
  end

  force = args.delete('--force') || args.delete('-f')
   = args.delete('--signup')
  pools = parse_pools(args)

  new(pools: pools, force: force, signup: ).generate
end

Instance Method Details

#generateObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/belt/cli/auth_command.rb', line 97

def generate
  check_collision! unless @force
  ensure_module_dir!

  write_cognito_tf
  write_cognito_outputs_tf
  patch_main_tf
  generate_frontend_auth if frontend?

  puts "\nāœ“ Auth generated!"
  puts "\nNext steps:"
  puts '  1. Review infrastructure/modules/app/cognito.tf'
  puts '  2. Customize password policy, MFA, or Lambda triggers as needed'
  puts '  3. Run `belt apply <env>` to deploy'
  puts '  4. Create your account: aws cognito-idp admin-create-user ...' unless @signup
end

#removeObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/belt/cli/auth_command.rb', line 114

def remove
  removed = []

  cognito_tf = File.join(MODULE_DIR, 'cognito.tf')
  cognito_outputs_tf = File.join(MODULE_DIR, 'cognito_outputs.tf')

  if File.exist?(cognito_tf)
    FileUtils.rm(cognito_tf)
    removed << cognito_tf
    puts "  remove  #{cognito_tf}"
  end

  if File.exist?(cognito_outputs_tf)
    FileUtils.rm(cognito_outputs_tf)
    removed << cognito_outputs_tf
    puts "  remove  #{cognito_outputs_tf}"
  end

  unpatch_main_tf
  removed << File.join(MODULE_DIR, 'main.tf') if @main_tf_patched

  if removed.empty?
    puts '  Nothing to remove — auth was not generated.'
  else
    puts "\nāœ“ Auth destroyed!"
  end
end