module Devise::Models::TokenAuthenticatable

def self.included(base)

def self.included(base)
  base.class_eval do
    extend ClassMethods
    before_save :ensure_authentication_token
  end
end

def ensure_authentication_token

Generate authentication token unless already exists.
def ensure_authentication_token
  self.reset_authentication_token if self.authentication_token.blank?
end

def ensure_authentication_token!

Generate authentication token unless already exists and save the record.
def ensure_authentication_token!
  self.reset_authentication_token! if self.authentication_token.blank?
end

def reset_authentication_token

Generate new authentication token (a.k.a. "single access token").
def reset_authentication_token
  self.authentication_token = self.class.authentication_token
end

def reset_authentication_token!

Generate new authentication token and save the record.
def reset_authentication_token!
  reset_authentication_token
  self.save
end

def valid_authentication_token?(incoming_auth_token)

is the user authentication token.
Verifies whether an +incoming_authentication_token+ (i.e. from single access URL)
def valid_authentication_token?(incoming_auth_token)
  incoming_auth_token.present? && incoming_auth_token == self.authentication_token
end