Files
sx-fc/app/controllers/concerns/token_verification.rb
2017-04-05 08:24:21 +06:30

37 lines
907 B
Ruby

module TokenVerification
extend ActiveSupport::Concern
included do
before_action :authenticate
end
protected
# Authenticate the user with token based authentication
def authenticate
authenticate_token || render_unauthorized
end
def authenticate_token
authenticate_with_http_token do |token, options|
#@current_user = User.find_by(api_key: token)
@device_access = DeviceAccess.find_by_token(token)
if @device_access
@log = DeviceAccessLog.new
@log.device_access = @device_access
@log.api_route = request.env['PATH_INFO']
@log.remote_ip = request.remote_ip
# @log.client_info =
@log.save
end
end
end
def render_unauthorized(realm = "Application")
self.headers["WWW-Authenticate"] = %(Token realm="#{realm.gsub(/"/, "")}")
render json: 'Bad credentials', status: :unauthorized
end
end