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 " 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_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 " 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_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 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_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
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_routes ⇒ Object
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_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 |