Module: OTP::JWT::ActiveRecord

Extended by:
ActiveSupport::Concern
Defined in:
lib/otp/jwt/active_record.rb

Overview

ActiveRecord

concern.

Instance Method Summary collapse

Instance Method Details

#expire_jwt?Boolean

Reset the [JWT] token if it is set to expire

This method allows you to expire any token, independently from the JWT flags/payload.

Returns:

  • (Boolean)

    nil if the expiration worked, otherwise returns the model



54
55
56
57
# File 'lib/otp/jwt/active_record.rb', line 54

def expire_jwt?
  return self unless self.respond_to?(:expire_jwt_at)
  return self unless expire_jwt_at? && expire_jwt_at.past?
end

#to_jwt(claims = nil) ⇒ ActiveRecord::Base

Returns a [JWT] token for this record

Parameters:

  • claims (Hash) (defaults to: nil)

    extra claims to be included

Returns:

  • (ActiveRecord::Base)

    model



41
42
43
44
45
46
# File 'lib/otp/jwt/active_record.rb', line 41

def to_jwt(claims = nil)
  OTP::JWT::Token.sign(
    sub: self.send(self.class.primary_key),
    **(claims || {})
  )
end