Skip to content

Instantly share code, notes, and snippets.

@arieliten
Last active August 29, 2015 14:16
Show Gist options
  • Save arieliten/37c9ea78971c02aef8bd to your computer and use it in GitHub Desktop.
Save arieliten/37c9ea78971c02aef8bd to your computer and use it in GitHub Desktop.
Custom authenticate
class Api::BaseController < ApplicationController
skip_before_filter :verify_authenticity_token
before_action :myTS_authenticate!
private
def myTS_authenticate!
if request.env['PATH_INFO'] =~ /api_data_sync/
data_sync_api_authentication
else
authenticate_user!
end
end
def data_sync_api_authentication
authenticate_or_request_with_http_token do |token, options|
if token == ENV['API_SYNC_TOKEN']
user = User.sync_data_user
sign_in(user, false)
end
end
end
end
Rails.application.routes.draw do
#...
scope :api_data_sync, defaults: { format: 'json' } do
scope :v1 do
resources :users, only: [:update], controller: 'api/v1/pd_resources', as: :api_sync_users
resources :pd_resources, controller: 'api/v1/pd_resources', as: :api_sync_pd_resources
end
end
# anything not matched by the above should be served the front-end index page
get "/*path" => "home#index"
end
class User < ActiveRecord::Base
#...
# == Class Methods
def self.sync_data_user
find_by(email: 'apisync_user@teachstone.org')
# TODO: should we create this user here if it's not present?
#find_or_initialize_by(email: 'apisync_user@teachstone.org')
#user.first_name = 'API Sync'
#user.last_name = 'User'
#user.password = 'testing'
#user.roles = Role.admin
#user.save
end
#...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment