2020-08-06 16:56:40 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
require './lib/knock_monkeypatch'
|
|
|
|
|
|
2020-05-31 22:38:19 +02:00
|
|
|
class Api::UserTokenController < Knock::AuthTokenController
|
2020-08-06 16:56:40 +00:00
|
|
|
include Oath::ControllerHelpers
|
2020-08-31 18:19:00 +02:00
|
|
|
include RememberMe::Controller
|
2020-08-06 16:56:40 +00:00
|
|
|
|
2020-05-31 22:38:19 +02:00
|
|
|
skip_before_action :verify_authenticity_token
|
2020-08-06 16:56:40 +00:00
|
|
|
before_action :sign_in_user
|
2020-05-31 22:38:19 +02:00
|
|
|
|
|
|
|
|
rescue_from Exception, :with => :return_error
|
|
|
|
|
|
|
|
|
|
# Catch exception and return JSON-formatted error
|
|
|
|
|
def return_error(exception)
|
2020-07-07 05:08:32 +02:00
|
|
|
Raven.capture_exception(exception)
|
|
|
|
|
|
2020-05-31 22:38:19 +02:00
|
|
|
logger.error "==Handled======="
|
|
|
|
|
logger.error exception.message
|
|
|
|
|
logger.error exception.backtrace.join("\n")
|
2020-08-06 16:56:40 +00:00
|
|
|
logger.error "==Handled======="
|
2020-05-31 22:38:19 +02:00
|
|
|
case exception
|
|
|
|
|
when ActiveRecord::RecordNotFound
|
|
|
|
|
@status = 404
|
|
|
|
|
@message = 'Record not found'
|
|
|
|
|
when ActiveRecord::RecordInvalid
|
|
|
|
|
@status = 422
|
|
|
|
|
@message = 'Record invalid'
|
|
|
|
|
when ArgumentError
|
|
|
|
|
@status = 400
|
|
|
|
|
@message = 'Argument Error'
|
|
|
|
|
else
|
|
|
|
|
@status = 500
|
|
|
|
|
@message = 'Internal Error'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# for some reason render json_errors is not working
|
2020-08-06 16:56:40 +00:00
|
|
|
# simulating JSON API support
|
|
|
|
|
render json: {
|
2020-05-31 22:38:19 +02:00
|
|
|
errors: [{
|
|
|
|
|
status: @status.to_s,
|
|
|
|
|
title: @message
|
|
|
|
|
}]
|
|
|
|
|
}
|
|
|
|
|
end
|
2020-08-06 16:56:40 +00:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def sign_in_user
|
|
|
|
|
sign_in(entity)
|
|
|
|
|
end
|
2020-05-31 22:38:19 +02:00
|
|
|
end
|