module SimpleTokenAuthentication::ActsAsTokenAuthenticatable

Public Instance Methods

ensure_authentication_token() click to toggle source

Set an authentication token if missing

Because it is intended to be used as a filter, this method is -and should be kept- idempotent.

# File lib/simple_token_authentication/acts_as_token_authenticatable.rb, line 21
def ensure_authentication_token
  if authentication_token.blank?
    self.authentication_token = generate_authentication_token(token_generator)
  end
end
generate_authentication_token(token_generator) click to toggle source
# File lib/simple_token_authentication/acts_as_token_authenticatable.rb, line 27
def generate_authentication_token(token_generator)
  loop do
    token = token_generator.generate_token
    break token if token_suitable?(token)
  end
end
token_generator() click to toggle source
# File lib/simple_token_authentication/acts_as_token_authenticatable.rb, line 38
def token_generator
  TokenGenerator.instance
end
token_suitable?(token) click to toggle source
# File lib/simple_token_authentication/acts_as_token_authenticatable.rb, line 34
def token_suitable?(token)
  self.class.unscoped.where(authentication_token: token).count == 0
end