Skip to content

Instantly share code, notes, and snippets.

@kzjeef
Created Mar 15, 2014
Embed
What would you like to do?
simple token auth user model, this is an comment code from : https://gist.github.com/gonzalo-bulnes/9001010
class User < ActiveRecord::Base
before_save :ensure_authentication_token
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :timeoutable,
:recoverable, :rememberable, :trackable, :validatable, :lockable
def ensure_authentication_token
if authentication_token.blank?
self.authentication_token = generate_authentication_token
end
end
def reset_authentication_token!
self.authentication_token = generate_authentication_token
end
private
def generate_authentication_token
loop do
token = Devise.friendly_token
break token unless User.where(authentication_token: token).first
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment