Class: Auth::JwtGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_routes ⇒ Object



29
30
31
32
33
34
# File 'lib/generators/auth/jwt_generator.rb', line 29

def add_routes
  route <<-ROUTES
  resources :users
  post "/auth/login", to: "authentication#login"
  ROUTES
end

#create_auth_files ⇒ Object



6
7
8
9
10
11
# File 'lib/generators/auth/jwt_generator.rb', line 6

def create_auth_files
  template "json_web_token.rb", "app/controllers/concerns/json_web_token.rb"
  template "authentication_controller.rb", "app/controllers/authentication_controller.rb"
  template "users_controller.rb", "app/controllers/users_controller.rb"
  template "user.rb", "app/models/user.rb"
end

#modify_application_controller ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/generators/auth/jwt_generator.rb', line 13

def modify_application_controller
  inject_into_class "app/controllers/application_controller.rb", "ApplicationController" do
    <<-RUBY
  include JsonWebToken
  before_action :authenticate_request
  private
  def authenticate_request
header = request.headers["Authorization"]
header = header.split(" ").last if header
decoded = jwt_decode(header)
@current_user = User.find(decoded[:user_id])
  end
    RUBY
  end
end