Module: SimpleTokenAuthentication::ActsAsTokenAuthenticatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/simple_token_authentication/acts_as_token_authenticatable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#ensure_authentication_tokenObject

Set an authentication token if missing

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



21
22
23
24
25
# 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) ⇒ Object



27
28
29
30
31
32
# 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_generatorObject



38
39
40
# File 'lib/simple_token_authentication/acts_as_token_authenticatable.rb', line 38

def token_generator
  TokenGenerator.instance
end

#token_suitable?(token) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/simple_token_authentication/acts_as_token_authenticatable.rb', line 34

def token_suitable?(token)
  self.class.where(authentication_token: token).count == 0
end