Update api_controller.rb
This commit is contained in:
parent
0861968e16
commit
10cca8e4b0
1 changed files with 37 additions and 2 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
class Api::V1::ApiController < ActionController::API
|
class Api::V1::ApiController < ActionController::API
|
||||||
|
##### Doorkeeper
|
||||||
include Doorkeeper::Rails::Helpers
|
include Doorkeeper::Rails::Helpers
|
||||||
|
|
||||||
|
##### Errors
|
||||||
rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response
|
rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response
|
||||||
rescue_from ActiveRecord::RecordNotDestroyed, with: :render_unprocessable_entity_response
|
rescue_from ActiveRecord::RecordNotDestroyed, with: :render_unprocessable_entity_response
|
||||||
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response
|
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response
|
||||||
|
|
@ -8,11 +10,39 @@ class Api::V1::ApiController < ActionController::API
|
||||||
rescue_from ActiveRecord::RecordNotUnique, with: :render_unprocessable_entity_response
|
rescue_from ActiveRecord::RecordNotUnique, with: :render_unprocessable_entity_response
|
||||||
rescue_from ActionController::ParameterMissing, with: :render_unprocessable_entity_response
|
rescue_from ActionController::ParameterMissing, with: :render_unprocessable_entity_response
|
||||||
|
|
||||||
|
##### Hooks
|
||||||
before_action :current_user
|
before_action :current_user
|
||||||
|
before_action :set_default_content_type
|
||||||
|
|
||||||
# Assign the current user if the Doorkeeper token isn't nil
|
##### Responders
|
||||||
|
respond_to :json
|
||||||
|
|
||||||
|
##### Methods
|
||||||
|
# Assign the current user if the Doorkeeper token isn't nil, then
|
||||||
|
# update the current user's last seen datetime and last IP address
|
||||||
|
# before returning
|
||||||
def current_user
|
def current_user
|
||||||
@current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
|
@current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
|
||||||
|
@current_user.update_last_ip_and_last_seen!(request.remote_ip) if @current_user
|
||||||
|
|
||||||
|
return @current_user
|
||||||
|
end
|
||||||
|
|
||||||
|
# Set the response content-type
|
||||||
|
def set_content_type(content_type)
|
||||||
|
response.headers["Content-Type"] = content_type
|
||||||
|
end
|
||||||
|
|
||||||
|
# Set the default response content-type to application/javascript
|
||||||
|
# with a UTF-8 charset
|
||||||
|
def set_default_content_type
|
||||||
|
set_content_type("application/javascript; charset=utf-8")
|
||||||
|
end
|
||||||
|
|
||||||
|
def current_user
|
||||||
|
@current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
|
||||||
|
@current_user.update_last_ip_and_last_seen!(request.remote_ip) if @current_user
|
||||||
|
|
||||||
return @current_user
|
return @current_user
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -22,7 +52,7 @@ class Api::V1::ApiController < ActionController::API
|
||||||
render action: 'errors', status: :unprocessable_entity
|
render action: 'errors', status: :unprocessable_entity
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_not_found_response(exception)
|
def render_not_found_response
|
||||||
response = { errors: [{ message: "Record could not be found.", code: "not_found" }]}
|
response = { errors: [{ message: "Record could not be found.", code: "not_found" }]}
|
||||||
render 'not_found', status: :not_found
|
render 'not_found', status: :not_found
|
||||||
end
|
end
|
||||||
|
|
@ -31,4 +61,9 @@ class Api::V1::ApiController < ActionController::API
|
||||||
render action: 'errors', status: :unauthorized
|
render action: 'errors', status: :unauthorized
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def restrict_access
|
||||||
|
raise UnauthorizedError unless current_user
|
||||||
|
end
|
||||||
end
|
end
|
||||||
Loading…
Reference in a new issue