Class: AhaBuilderCore::AuthGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- AhaBuilderCore::AuthGenerator
- Includes:
- ActiveRecord::Generators::Migration
- Defined in:
- lib/generators/aha_builder_core/auth/auth_generator.rb
Instance Method Summary collapse
- #add_authentication_to_application_controller ⇒ Object
- #add_inertia_auth_share ⇒ Object
- #add_routes ⇒ Object
- #add_typescript_types ⇒ Object
- #create_authentication_concern ⇒ Object
- #create_current_model ⇒ Object
- #create_sessions_controller ⇒ Object
- #create_user_model ⇒ Object
- #display_instructions ⇒ Object
- #generate_js_routes ⇒ Object
- #generate_migration_file ⇒ Object
Instance Method Details
#add_authentication_to_application_controller ⇒ Object
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_share ⇒ Object
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 <<-RUBY inertia_share auth: -> { { user: current_user&.as_json(only: %i[id email first_name last_name]) } } RUBY end end |
#add_routes ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 38 def add_routes route <<~RUBY get "login", to: "sessions#new", as: :new_session get "callback", to: "sessions#callback", as: :session_callback delete "logout", to: "sessions#logout", as: :logout RUBY end |
#add_typescript_types ⇒ Object
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 |
# 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\}/, <<~TYPESCRIPT.chomp export interface Flash { alert?: string notice?: string } export interface AuthUser { id: number email: string first_name: string | null last_name: string | null } export interface SharedData { flash: Flash auth: { user: AuthUser | null } [key: string]: unknown } TYPESCRIPT end |
#create_authentication_concern ⇒ Object
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_model ⇒ Object
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_controller ⇒ Object
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_model ⇒ Object
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_instructions ⇒ Object
102 103 104 |
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 102 def display_instructions say "\nAha Auth setup complete!", :green end |
#generate_js_routes ⇒ Object
97 98 99 100 |
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 97 def generate_js_routes say "\nRegenerating js-routes file...", :green rails_command "js:routes" end |
#generate_migration_file ⇒ Object
16 17 18 19 20 |
# File 'lib/generators/aha_builder_core/auth/auth_generator.rb', line 16 def generate_migration_file return if [:skip_migration] migration_template "migration.rb.tt", File.join(db_migrate_path, "create_users.rb") end |