Class: ApplicationController

Inherits:
ActionController::API
  • Object
show all
Defined in:
lib/generators/jwt_rails/templates/application_controller.rb

Direct Known Subclasses

AuthenticationController, UsersController

Instance Method Summary collapse

Instance Method Details

#authorize_requestObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/generators/jwt_rails/templates/application_controller.rb', line 9

def authorize_request
  header = request.headers['Authorization']
  header = header.split(' ').last if header
  begin
    @decoded = JsonWebToken.decode(header)
    @current_user = User.find(@decoded[:user_id])
  rescue ActiveRecord::RecordNotFound => e
    render json: { errors: e.message }, status: :unauthorized
  rescue JWT::DecodeError => e
    render json: { errors: e.message }, status: :unauthorized
  end
end

#is_owner(user_id) ⇒ Object



22
23
24
25
26
27
# File 'lib/generators/jwt_rails/templates/application_controller.rb', line 22

def is_owner user_id
  unless user_id == current_user.id
    render json: nil, status: :forbidden
    return
  end
end

#is_owner_object(data) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/generators/jwt_rails/templates/application_controller.rb', line 29

def is_owner_object data
  if data.nil? or data.user_id.nil?
    return render status: :not_found
  else
    is_owner data.user_id
  end
end

#not_foundObject



5
6
7
# File 'lib/generators/jwt_rails/templates/application_controller.rb', line 5

def not_found
  render json: { error: 'not_found' }
end