Class: Spree::RefreshToken
- Inherits:
-
Object
- Object
- Spree::RefreshToken
- Defined in:
- app/models/spree/refresh_token.rb
Class Method Summary collapse
-
.cleanup_expired! ⇒ Object
Clean up expired tokens.
-
.create_for(user, request_env: {}) ⇒ Object
Create a refresh token for a user.
- .default_expiry ⇒ Object
-
.revoke_all_for(user) ⇒ Object
Revoke all refresh tokens for a user (e.g., on password change).
Instance Method Summary collapse
- #expired? ⇒ Boolean
-
#rotate!(request_env: {}) ⇒ Object
Rotate: destroy this token and create a new one.
Class Method Details
.cleanup_expired! ⇒ Object
Clean up expired tokens
50 51 52 |
# File 'app/models/spree/refresh_token.rb', line 50 def self.cleanup_expired! expired.delete_all end |
.create_for(user, request_env: {}) ⇒ Object
Create a refresh token for a user
35 36 37 38 39 40 41 42 |
# File 'app/models/spree/refresh_token.rb', line 35 def self.create_for(user, request_env: {}) create!( user: user, expires_at: default_expiry.from_now, ip_address: request_env[:ip_address], user_agent: request_env[:user_agent] ) end |
.default_expiry ⇒ Object
54 55 56 57 58 |
# File 'app/models/spree/refresh_token.rb', line 54 def self.default_expiry Spree::Api::Config[:refresh_token_expiry].seconds rescue StandardError 30.days end |
.revoke_all_for(user) ⇒ Object
Revoke all refresh tokens for a user (e.g., on password change)
45 46 47 |
# File 'app/models/spree/refresh_token.rb', line 45 def self.revoke_all_for(user) where(user: user).delete_all end |
Instance Method Details
#expired? ⇒ Boolean
14 15 16 |
# File 'app/models/spree/refresh_token.rb', line 14 def expired? expires_at <= Time.current end |
#rotate!(request_env: {}) ⇒ Object
Rotate: destroy this token and create a new one. Returns the new token.
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/spree/refresh_token.rb', line 20 def rotate!(request_env: {}) new_token = nil transaction do new_token = self.class.create!( user: user, expires_at: self.class.default_expiry.from_now, ip_address: request_env[:ip_address] || ip_address, user_agent: request_env[:user_agent] || user_agent ) destroy! end new_token end |