Skip to content

Instantly share code, notes, and snippets.

@mhuggins
Last active July 28, 2017 02:13
Show Gist options
  • Save mhuggins/dc56ca53ac2ca5c89967 to your computer and use it in GitHub Desktop.
Save mhuggins/dc56ca53ac2ca5c89967 to your computer and use it in GitHub Desktop.
Devise authentication via Authentication token header (untested)
class ApplicationController < ActionController::Base
before_filter :authenticate_user_from_token!
private
def authenticate_user_from_token!
authenticate_or_request_with_http_token do |token, options|
user = User.find_by_authentication_token(token)
if user && Devise.secure_compare(user.authentication_token, token)
sign_in user, store: false
end
user.present?
end
end
end
require 'active_support/concern'
module TokenAuthenticatable
extend ActiveSupport::Concern
included do
before_validation :generate_authentication_token, on: :create
before_save :change_authentication_token, on: :update
end
private
def generate_authentication_token
self.authentication_token = Devise.token_generator.generate(self.class, :authentication_token)
end
def change_authentication_token
generate_authentication_token if encrypted_password_changed?
end
end
class User
include TokenAuthenticatable
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment