Last Updated: February 25, 2016
·
709
· ianrgz

Quick token authentication for rails controllers

Create a concern in concerns/request_auth.rb with the following:

module RequestAuth
  extend ActiveSupport::Concern

  included do
    before_filter :authenticate_request
  end

  def authenticate_request
    raise "Invalid authentication token" if params[:auth_token] != "some_auth_token"
  end
end

Then in the controller you wish to secure just include the module.

class SomeController < ApplicationController
  include RequestAuth
  ...
end

And that's it your controller should be secured by the auth token you specify.