Sha256: f37e55556964bfc8d86ab2f58491c5e895aad6106f078b952b5b38273d1fc496

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

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

1 entries across 1 versions & 1 rubygems

Version Path
solidus_jwt-1.1.0 app/models/solidus_jwt/token.rb