Last active
August 29, 2015 14:16
-
-
Save arieliten/37c9ea78971c02aef8bd to your computer and use it in GitHub Desktop.
Custom authenticate
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 | |
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 |
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
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 |
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 User < ActiveRecord::Base | |
#... | |
# == Class Methods | |
def self.sync_data_user | |
find_by(email: '[email protected]') | |
# TODO: should we create this user here if it's not present? | |
#find_or_initialize_by(email: '[email protected]') | |
#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