Sha256: 78f4c8e9c2f61f21d5c775496c8e1fa2787b0f10a5d197d71a8cbbbba37206ef

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module SolidusJwt
  class Token < ApplicationRecord
    attr_readonly :token
    enum auth_type: %i[refresh_token access_token]

    belongs_to :user, class_name: Spree::UserClassHandle.new

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

    enum auth_type: %i[refresh access]

    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)
      non_expired.
        where(user_id: user.to_param).
        update_all(active: false)
    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

    def honor?
      active? && !expired?
    end

    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.0.0 app/models/solidus_jwt/token.rb
solidus_jwt-1.0.0.beta2 app/models/solidus_jwt/token.rb
solidus_jwt-1.0.0.beta1 app/models/solidus_jwt/token.rb