Created
March 15, 2014 11:05
-
-
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
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 < 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