class ApplicationController

Public Instance Methods

authorize_request() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_found() click to toggle source
# File lib/generators/jwt_rails/templates/application_controller.rb, line 5
def not_found
  render json: { error: 'not_found' }
end