Class: AuthGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- AuthGenerator
- Includes:
- Rails::Generators::Migration
- Defined in:
- lib/generators/auth/auth_generator.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_routes ⇒ Object
- #copy_migration ⇒ Object
- #create_auth_files ⇒ Object
- #enable_cors ⇒ Object
- #modify_application_controller ⇒ Object
- #modify_application_rb ⇒ Object
- #modify_gemfile ⇒ Object
Class Method Details
.next_migration_number(dirname) ⇒ Object
63 64 65 66 67 |
# File 'lib/generators/auth/auth_generator.rb', line 63 def self.next_migration_number(dirname) @prev_migration_nr ||= Time.now.utc.strftime("%Y%m%d%H%M%S").to_i @prev_migration_nr += 1 @prev_migration_nr.to_s end |
Instance Method Details
#add_routes ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/generators/auth/auth_generator.rb', line 25 def add_routes route <<~RUBY # config/routes.rb post '/login', to: 'auth#login' post '/refresh', to: 'auth#refresh' post '/logout', to: 'auth#logout' post '/signup', to: 'users#create' get '/me', to: 'users#me' resources :password_resets, only: [:create] do collection do put '/', to: 'password_resets#update' # PUT /password_resets end end # Admin routes post '/users/:id/make_admin', to: 'users#make_admin' RUBY end |
#copy_migration ⇒ Object
69 70 71 72 |
# File 'lib/generators/auth/auth_generator.rb', line 69 def copy_migration migration_template "migrations/create_user.rb", "db/migrate/create_users.rb" migration_template "migrations/create_refresh_token.rb", "db/migrate/create_refresh_tokens.rb" end |
#create_auth_files ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/generators/auth/auth_generator.rb', line 43 def create_auth_files template "controllers/auth_controller.rb", "app/controllers/auth_controller.rb" template "serializers/user_serializer.rb", "app/serializers/user_serializer.rb" template "controllers/users_controller.rb", "app/controllers/users_controller.rb" template "controllers/password_resets_controller.rb", "app/controllers/password_resets_controller.rb" template "models/user.rb", "app/models/user.rb" template "models/refresh_token.rb", "app/models/refresh_token.rb" template "mailers/user_mailer.rb", "app/mailers/user_mailer.rb" template "mailers/application_mailer.rb", "app/mailers/application_mailer.rb" template "concerns/authenticatable.rb", "app/controllers/concerns/authenticatable.rb" template "initializers/rails_auth_generator.rb", "config/initializers/rails_auth_generator.rb" end |
#enable_cors ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/generators/auth/auth_generator.rb', line 74 def enable_cors insert_into_file "config/application.rb"do <<~RUBY Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins "example.com" resource "*", headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head] end end RUBY end end |
#modify_application_controller ⇒ Object
56 57 58 59 60 |
# File 'lib/generators/auth/auth_generator.rb', line 56 def modify_application_controller inject_into_class "app/controllers/application_controller.rb", "ApplicationController" do " include Authenticatable\n" end end |
#modify_application_rb ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/generators/auth/auth_generator.rb', line 16 def modify_application_rb insert_into_file "config/application.rb", after: "config.api_only = true\n" do <<~RUBY config.middleware.use ActionDispatch::Cookies RUBY end end |
#modify_gemfile ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/generators/auth/auth_generator.rb', line 5 def modify_gemfile insert_into_file "Gemfile", after: /^source ['"].*['"]\n/ do <<~RUBY gem 'bcrypt', '~> 3.1', '>= 3.1.12' gem 'jwt', '~> 2.5' gem 'rack-cors' gem 'active_model_serializers', '~> 0.10.12' RUBY end end |