Class: AuthGenerator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



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

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_routesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/auth/auth_generator.rb', line 25

def add_routes
  route "    # config/routes.rb\n      post '/login', to: 'auth#login'\n      post '/refresh', to: 'auth#refresh'\n      post '/logout', to: 'auth#logout'\n      post '/signup', to: 'users#create'\n      get '/me', to: 'users#me'\n      resources :password_resets, only: [:create] do\n        collection do\n          put '/', to: 'password_resets#update'  # PUT /password_resets\n        end\n      end\n  RUBY\nend\n"

#copy_migrationObject



67
68
69
70
# File 'lib/generators/auth/auth_generator.rb', line 67

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_filesObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/generators/auth/auth_generator.rb', line 41

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/jwt_rails_api_auth.rb", "config/initializers/jwt_rails_api_auth.rb"
end

#enable_corsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/generators/auth/auth_generator.rb', line 72

def enable_cors
  insert_into_file "config/application.rb"do
    "      Rails.application.config.middleware.insert_before 0, Rack::Cors do\n        allow do\n          origins \"example.com\"\n\n          resource \"*\",\n            headers: :any,\n            methods: [:get, :post, :put, :patch, :delete, :options, :head]\n        end\n      end\n    RUBY\n  end\nend\n"

#modify_application_controllerObject



54
55
56
57
58
# File 'lib/generators/auth/auth_generator.rb', line 54

def modify_application_controller
  inject_into_class "app/controllers/application_controller.rb", "ApplicationController" do
    "  include Authenticatable\n"
  end
end

#modify_application_rbObject



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
    "      config.middleware.use ActionDispatch::Cookies\n    RUBY\n  end\n\nend\n"

#modify_gemfileObject



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
    "      gem \"bcrypt\", \">= 3.1.12\"\n      gem \"jwt\", \">= 2.5\"\n      gem \"rack-cors\", \">= 0\"\n      gem \"active_model_serializers\", \">= 0.10.12\"\n    RUBY\n  end\nend\n"