Skip to content

Instantly share code, notes, and snippets.

@kzjeef
Created March 15, 2014 11:05
Show Gist options
  • Save kzjeef/9565261 to your computer and use it in GitHub Desktop.
Save kzjeef/9565261 to your computer and use it in GitHub Desktop.
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