Last active
July 28, 2017 02:13
-
-
Save mhuggins/dc56ca53ac2ca5c89967 to your computer and use it in GitHub Desktop.
Devise authentication via Authentication token header (untested)
This file contains 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 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 |
This file contains 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
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 |
This file contains 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 | |
include TokenAuthenticatable | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment