Class: PasskeyAuth::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActiveRecord::Generators::Migration
Defined in:
lib/generators/passkey_auth/install/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_routesObject



31
32
33
# File 'lib/generators/passkey_auth/install/install_generator.rb', line 31

def add_routes
  route 'mount PasskeyAuth::Engine => "/auth"'
end

#copy_javascript_filesObject



35
36
37
38
39
# File 'lib/generators/passkey_auth/install/install_generator.rb', line 35

def copy_javascript_files
  # Copy JavaScript controllers from the gem's app/javascript directory
  source_paths << File.expand_path("../../../../app/javascript", __dir__)
  directory "passkey_auth", "app/javascript/passkey_auth"
end

#copy_migrationsObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/generators/passkey_auth/install/install_generator.rb', line 15

def copy_migrations
  migration_template "create_passkey_auth_magic_links.rb.erb",
                    "db/migrate/create_passkey_auth_magic_links.rb",
                    migration_version: migration_version
  migration_template "create_passkey_auth_webauthn_credentials.rb.erb",
                    "db/migrate/create_passkey_auth_webauthn_credentials.rb",
                    migration_version: migration_version
  migration_template "add_passwordless_to_users.rb.erb",
                    "db/migrate/add_passwordless_to_users.rb",
                    migration_version: migration_version
end

#create_initializerObject



27
28
29
# File 'lib/generators/passkey_auth/install/install_generator.rb', line 27

def create_initializer
  template "initializer.rb", "config/initializers/passkey_auth.rb"
end

#display_post_install_messageObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/generators/passkey_auth/install/install_generator.rb', line 67

def display_post_install_message
  say "\n"
  say "PasskeyAuth installed successfully!", :green
  say "\n"
  say "Next steps:", :yellow
  say "  1. Ensure you have Rails authentication installed (rails generate authentication)"
  say "  2. Add 'include PasskeyAuth::Concerns::Passwordless' to your User model"
  say "  3. Run 'rails db:migrate' to create the necessary tables"
  say "  4. Configure the initializer at config/initializers/passkey_auth.rb"
  say "  5. Register Stimulus controllers in app/javascript/controllers/application.js:"
  say ""
  say "     import { PasskeyAuthenticationController, WebauthnRegisterController, WebauthnAuthController }", :cyan
  say "       from 'passkey_auth/controllers'", :cyan
  say "     application.register('passkey-authentication', PasskeyAuthenticationController)", :cyan
  say "     application.register('webauthn-register', WebauthnRegisterController)", :cyan
  say "     application.register('webauthn-auth', WebauthnAuthController)", :cyan
  say "\n"
  say "PasskeyAuth works alongside Rails' authentication system.", :yellow
  say "Users can authenticate with passwords (Rails) OR passwordless methods (this gem)."
  say "\n"
  say "See the README for more details: https://github.com/jhubert/passkey_auth"
  say "\n"
end

#install_javascript_dependenciesObject



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
# File 'lib/generators/passkey_auth/install/install_generator.rb', line 41

def install_javascript_dependencies
  say "\nInstalling JavaScript dependencies...", :yellow

  if File.exist?("config/importmap.rb")
    append_to_file "config/importmap.rb" do
      <<~RUBY

        # PasskeyAuth dependencies
        pin "@github/webauthn-json", to: "https://ga.jspm.io/npm:@github/[email protected]/dist/esm/webauthn-json.js"
        pin "@rails/request.js", to: "https://ga.jspm.io/npm:@rails/[email protected]/src/index.js"
        pin "passkey_auth/controllers", to: "passkey_auth/controllers/index.js"
        pin "passkey_auth/controllers/passkey_authentication_controller"
        pin "passkey_auth/controllers/webauthn_register_controller"
        pin "passkey_auth/controllers/webauthn_auth_controller"
        pin "passkey_auth/lib/passkey"
      RUBY
    end
    say "Added importmap pins for PasskeyAuth", :green
  else
    say "\nWARNING: config/importmap.rb not found.", :yellow
    say "If you're using a different JavaScript bundler (esbuild, webpack, etc.),"
    say "make sure to add @github/webauthn-json as a dependency:", :yellow
    say "\n  npm install @github/webauthn-json\n", :cyan
  end
end