app/models/solidus_jwt/token.rb in solidus_jwt-1.0.0 vs app/models/solidus_jwt/token.rb in solidus_jwt-1.1.0

- old
+ new

@@ -1,20 +1,22 @@ module SolidusJwt - class Token < ApplicationRecord + class Token < BaseRecord attr_readonly :token - enum auth_type: %i[refresh_token access_token] + enum auth_type: { refresh_token: 0, access_token: 1 } - belongs_to :user, class_name: Spree::UserClassHandle.new + # 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: %i[refresh access] + enum auth_type: { refresh: 0, access: 1 } validates :token, presence: true before_validation(on: :create) do self.token ||= SecureRandom.uuid @@ -22,26 +24,37 @@ ## # 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