Module: Janus::Models::TokenAuthenticatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/janus/models/token_authenticatable.rb

Overview

TokenAuthenticatable

Allows to connect through an unique identifier.

The strategy to generate the authentication token is up to you. You may either generate a token when a user is created:

before_create :reset_authentication_token

or you may change the token whenever a user is saved:

before_save :reset_authentification_token

or whenever its password is changed:

before_save :reset_authentication_token, :if => :encrypted_password_changed?

The strategy to invalidate the authentication token is also up to you. You may use a callback or one of the configuration options:

  • token_authentication_valid_for - number of seconds a token will be valid once created (defaults to nil);

  • reusable_authentication_token - false to destroy the token once it’s consumed (defaults to true).

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroy_authentication_token!Object

Destroys the auth token.



56
57
58
# File 'lib/janus/models/token_authenticatable.rb', line 56

def destroy_authentication_token!
  update_attribute(:authentication_token, nil)
end

#reset_authentication_tokenObject

Generates an unique authentification token.



50
51
52
53
# File 'lib/janus/models/token_authenticatable.rb', line 50

def reset_authentication_token
  self.authentication_token = self.class.generate_token(:authentication_token)
  self.authentication_token_created_at = Time.now
end

#reset_authentication_token!Object

Generates an unique authentication token and saves the model. Any existing token will be overwritten.



44
45
46
47
# File 'lib/janus/models/token_authenticatable.rb', line 44

def reset_authentication_token!
  reset_authentication_token
  save
end