Sha256: c79ceac52758e77c81eec39d222cb80fe4bf6e35368f7c36798e4864e5fe3ca8

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module SolidusJwt
  class Token < BaseRecord
    attr_readonly :token
    enum auth_type: { refresh_token: 0, access_token: 1 }

    # rubocop:disable Rails/ReflectionClassName
    belongs_to :user, class_name: ::Spree::UserClassHandle.new
    # rubocop:enable Rails/ReflectionClassName

    scope :non_expired, -> {
      where(
        'solidus_jwt_tokens.created_at >= ?',
        SolidusJwt::Config.refresh_expiration.seconds.ago
      )
    }

    enum auth_type: { refresh: 0, access: 1 }

    validates :token, presence: true

    before_validation(on: :create) do
      self.token ||= SecureRandom.uuid
    end

    ##
    # Set all  non expired refresh tokens to inactive
    #
    def self.invalidate(user)
      # rubocop:disable Rails/SkipsModelValidations
      non_expired.
        where(user_id: user.to_param).
        update_all(active: false)
      # rubocop:enable Rails/SkipsModelValidations
    end

    ##
    # Whether to honor a token or not.
    # @return [Boolean]
    #
    def self.honor?(token)
      non_expired.where(active: true).find_by(token: token).present?
    end

    ##
    # Whether the token should be honored.
    # @return [Boolean] Will be true if the token is active and not expired.
    #   Otherwise false.
    def honor?
      active? && !expired?
    end

    ##
    # Whether the token is expired
    # @return [Boolean] If the token is older than the configured refresh
    #   expiration amount then will be true. Otherwise false.
    #
    def expired?
      created_at < SolidusJwt::Config.refresh_expiration.seconds.ago
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_jwt-1.2.2 app/models/solidus_jwt/token.rb
solidus_jwt-1.2.1 app/models/solidus_jwt/token.rb
solidus_jwt-1.2.0 app/models/solidus_jwt/token.rb