Module: Jm81auth::Models::AuthToken
- Defined in:
- lib/jm81auth/models/auth_token.rb
Defined Under Namespace
Modules: ClassMethods Classes: DecodeError
Class Method Summary collapse
Instance Method Summary collapse
-
#close! ⇒ Object
Set #closed_at.
-
#encoded ⇒ String
{ auth_token_id: self.id } encoded via JWT for passing to client.
-
#expired? ⇒ Boolean
Is this token expired?.
-
#expires_at ⇒ DateTime
Time when this token expires.
-
#open? ⇒ Boolean
True if token is not expired or closed.
Class Method Details
.included(base) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/jm81auth/models/auth_token.rb', line 4 def self.included(base) base.extend ClassMethods base.class_eval do if respond_to? :belongs_to belongs_to :auth_method belongs_to :user else plugin :timestamps many_to_one :auth_method many_to_one :user end end end |
Instance Method Details
#close! ⇒ Object
Set #closed_at. Called, for example, when logging out.
24 25 26 27 28 29 30 31 32 |
# File 'lib/jm81auth/models/auth_token.rb', line 24 def close! unless self.closed_at if respond_to? :update_attributes! self.update_attributes! closed_at: Time.now else self.update closed_at: Time.now end end end |
#encoded ⇒ String
Returns { auth_token_id: self.id } encoded via JWT for passing to client.
36 37 38 |
# File 'lib/jm81auth/models/auth_token.rb', line 36 def encoded self.class.encode auth_token_id: self.id end |
#expired? ⇒ Boolean
Returns Is this token expired?.
41 42 43 |
# File 'lib/jm81auth/models/auth_token.rb', line 41 def expired? !open? end |
#expires_at ⇒ DateTime
Returns Time when this token expires.
51 52 53 |
# File 'lib/jm81auth/models/auth_token.rb', line 51 def expires_at last_used_at + self.class.config.expires_seconds end |
#open? ⇒ Boolean
Returns True if token is not expired or closed.
46 47 48 |
# File 'lib/jm81auth/models/auth_token.rb', line 46 def open? !(last_used_at.nil?) && !closed_at && Time.now <= expires_at end |