Skip to content

Instantly share code, notes, and snippets.

@jackcallister
Created April 29, 2014 12:13
Show Gist options
  • Save jackcallister/11398400 to your computer and use it in GitHub Desktop.
Save jackcallister/11398400 to your computer and use it in GitHub Desktop.
API Base Controller
class API::BaseController < ApplicationController
respond_to :json
before_action :set_default_format, :assert_valid_format!, :authenticate_from_token!, :authenticate_client!
protected
def authenticate_from_token!
token = request.headers['X-Authentication-Token']
id = request.headers['X-Authentication-Id']
resource = User.find_by(authentication_token: token, id: id) if token && id
sign_in resource, store: false if resource
end
private
def set_default_format
request.format = 'json'
end
def assert_valid_format!
raise ActionController::UnknownFormat unless request.format == 'json'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment