Class: AhaBuilderCore::AuthGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActiveRecord::Generators::Migration
Defined in:
lib/generators/aha_builder_core/auth/auth_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_authentication_to_application_controllerObject



61
62
63
64
65
66
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 61

def add_authentication_to_application_controller
  inject_into_file "app/controllers/application_controller.rb",
    after: "class ApplicationController < ActionController::Base\n" do
      "  include Authentication\n"
    end
end

#add_inertia_auth_shareObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 46

def add_inertia_auth_share
  return unless File.exist?("app/controllers/inertia_controller.rb")

  inject_into_file "app/controllers/inertia_controller.rb",
    after: "inertia_share flash: -> { flash.to_hash }\n" do
      "  inertia_share auth: -> {\n{\n  user: current_user&.as_json(only: %i[id email first_name last_name avatar_url])\n}\n  }\n      RUBY\n    end\nend\n"

#add_routesObject



38
39
40
41
42
43
44
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 38

def add_routes
  route "    get \"login\", to: \"sessions#new\", as: :login\n    get \"callback\", to: \"sessions#callback\", as: :login_callback\n    get \"logout\", to: \"sessions#logout\", as: :logout\n  RUBY\nend\n"

#add_typescript_typesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 68

def add_typescript_types
  types_file = "app/frontend/types/index.ts"
  return unless File.exist?(types_file)

  gsub_file types_file,
    /export interface Flash \{\n  alert\?: string\n  notice\?: string\n\}\n\nexport interface SharedData \{\n  flash: Flash\n  \[key: string\]: unknown\n\}/,
    "      export interface Flash {\n        alert?: string\n        notice?: string\n      }\n\n      export interface AuthUser {\n        id: number\n        email: string\n        first_name: string | null\n        last_name: string | null\n        avatar_url: string | null\n      }\n\n      export interface SharedData {\n        flash: Flash\n        auth: {\n          user: AuthUser | null\n        }\n        [key: string]: unknown\n      }\n    TYPESCRIPT\nend\n".chomp

#create_authentication_concernObject



34
35
36
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 34

def create_authentication_concern
  template "authentication.rb.tt", "app/controllers/concerns/authentication.rb"
end

#create_current_modelObject



26
27
28
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 26

def create_current_model
  template "current.rb.tt", "app/models/current.rb"
end

#create_sessions_controllerObject



30
31
32
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 30

def create_sessions_controller
  template "sessions_controller.rb.tt", "app/controllers/sessions_controller.rb"
end

#create_user_modelObject



22
23
24
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 22

def create_user_model
  template "user.rb.tt", "app/models/user.rb"
end

#display_instructionsObject



103
104
105
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 103

def display_instructions
  say "\nAha Auth setup complete!", :green
end

#generate_js_routesObject



98
99
100
101
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 98

def generate_js_routes
  say "\nRegenerating js-routes file...", :green
  rails_command "js:routes"
end

#generate_migration_fileObject



16
17
18
19
20
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 16

def generate_migration_file
  return if options[:skip_migration]

  migration_template "migration.rb.tt", File.join(db_migrate_path, "create_users.rb")
end