Created
April 29, 2014 12:13
-
-
Save jackcallister/11398400 to your computer and use it in GitHub Desktop.
API Base Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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