module Jm81auth::Models::AuthToken

Public Class Methods

included(base) click to toggle source
# File lib/jm81auth/models/auth_token.rb, line 4
def self.included(base)
  base.extend ClassMethods

  base.class_eval do
    if respond_to? :belongs_to
      belongs_to :auth_method, required: false
      belongs_to :user, required: false
    else
      plugin :timestamps

      many_to_one :auth_method
      many_to_one :user
    end
  end
end

Public Instance Methods

close!() click to toggle source

Set closed_at. Called, for example, when logging out.

# File lib/jm81auth/models/auth_token.rb, line 24
def close!
  unless self.closed_at
    if respond_to? :update_attributes!
      self.update_attributes! closed_at: Time.now
    else
      self.update closed_at: Time.now
    end
  end
end
encoded() click to toggle source

@return [String]

{ auth_token_id: self.id } encoded via JWT for passing to client.
# File lib/jm81auth/models/auth_token.rb, line 36
def encoded
  self.class.encode auth_token_id: self.id
end
expired?() click to toggle source

@return [Boolean] Is this token expired?

# File lib/jm81auth/models/auth_token.rb, line 41
def expired?
  !open?
end
expires_at() click to toggle source

@return [DateTime] Time when this token expires.

# File lib/jm81auth/models/auth_token.rb, line 51
def expires_at
  last_used_at + self.class.config.expires_seconds
end
open?() click to toggle source

@return [Boolean] True if token is not expired or closed.

# File lib/jm81auth/models/auth_token.rb, line 46
def open?
  !(last_used_at.nil?) && !closed_at && Time.now <= expires_at
end