Class: Parse::Session
- Inherits:
-
Object
- Object
- Object
- Parse::Session
- Defined in:
- lib/parse/model/classes/session.rb,
lib/parse/stack/generators/templates/model_session.rb
Overview
This class represents the data and columns contained in the standard Parse
_Session collection. The Session class maintains per-device (or website) authentication
information for a particular user. Whenever a User object is logged in, a new Session record, with
a session token is generated. You may use a known active session token to find the corresponding
user for that session. Deleting a Session record (and session token), effectively logs out the user, when making Parse requests
on behalf of the user using the session token.
The default schema for the Session class is as follows:
class Parse::Session < Parse::Object
# See Parse::Object for inherited properties...
property :session_token
property :created_with, :object
property :expires_at, :date
property :installation_id
property :restricted, :boolean
belongs_to :user
# Installation where the installation_id matches.
has_one :installation, ->{ where(installation_id: i.installation_id) }, scope_only: true
end
Instance Attribute Summary collapse
-
#created_with ⇒ Hash
Data on how this Session was created.
-
#expires_at ⇒ Parse::Date
When the session token expires.
-
#installation ⇒ Parse::Installation
readonly
Returns the Installation where the sessions installation_id field matches the installation_id field in the Installation collection.
-
#installation_id ⇒ String
The installation id from the Installation table.
-
#restricted ⇒ Boolean
readonly
Whether this session token is restricted.
-
#session_token ⇒ String
readonly
The session token for this installation and user pair.
-
#user ⇒ User
readonly
This property is mapped as a
belongs_toassociation with the User class.
Class Method Summary collapse
-
.active ⇒ Parse::Query
Query scope for active (non-expired) sessions.
-
.active_count_for_user(user) ⇒ Integer
Count active sessions for a specific user.
-
.expired ⇒ Parse::Query
Query scope for expired sessions.
-
.for_user(user) ⇒ Parse::Query
Query scope for sessions belonging to a specific user.
-
.revoke_all_for_user(user, except: nil) ⇒ Integer
Revoke (delete) all sessions for a specific user.
-
.session(token, **opts) ⇒ Session
Return the Session record for this session token.
Instance Method Summary collapse
-
#expired? ⇒ Boolean
Check if this session has expired.
-
#expires_within?(duration) ⇒ Boolean
Check if this session expires within the given duration.
-
#revoke! ⇒ Boolean
Revoke (delete) this session, effectively logging out the user on this device.
-
#time_remaining ⇒ Float?
Get the remaining time until this session expires.
-
#valid? ⇒ Boolean
Check if this session is still valid (not expired).
Instance Attribute Details
#created_with ⇒ Hash
Returns data on how this Session was created.
36 |
# File 'lib/parse/model/classes/session.rb', line 36 property :created_with, :object |
#expires_at ⇒ Parse::Date
Returns when the session token expires.
40 |
# File 'lib/parse/model/classes/session.rb', line 40 property :expires_at, :date |
#installation ⇒ Parse::Installation (readonly)
Returns the Installation where the sessions installation_id field matches the installation_id field in the Installation collection. This is implemented as a has_one scope.
66 |
# File 'lib/parse/model/classes/session.rb', line 66 has_one :installation, -> { where(installation_id: i.installation_id) }, scope_only: true |
#installation_id ⇒ String
Returns The installation id from the Installation table.
45 |
# File 'lib/parse/model/classes/session.rb', line 45 property :installation_id |
#restricted ⇒ Boolean (readonly)
Returns whether this session token is restricted.
49 |
# File 'lib/parse/model/classes/session.rb', line 49 property :restricted, :boolean |
#session_token ⇒ String (readonly)
Returns the session token for this installation and user pair.
53 |
# File 'lib/parse/model/classes/session.rb', line 53 property :session_token |
Class Method Details
.active ⇒ Parse::Query
Query scope for active (non-expired) sessions.
95 96 97 |
# File 'lib/parse/model/classes/session.rb', line 95 def active query(:expires_at.gte => Time.now) end |
.active_count_for_user(user) ⇒ Integer
Count active sessions for a specific user.
140 141 142 |
# File 'lib/parse/model/classes/session.rb', line 140 def active_count_for_user(user) for_user(user).where(:expires_at.gte => Time.now).count end |
.expired ⇒ Parse::Query
Query scope for expired sessions.
103 104 105 |
# File 'lib/parse/model/classes/session.rb', line 103 def expired query(:expires_at.lt => Time.now) end |
.for_user(user) ⇒ Parse::Query
Query scope for sessions belonging to a specific user.
112 113 114 115 |
# File 'lib/parse/model/classes/session.rb', line 112 def for_user(user) user = Parse::User.pointer(user) if user.is_a?(String) query(user: user) end |
.revoke_all_for_user(user, except: nil) ⇒ Integer
Revoke (delete) all sessions for a specific user.
127 128 129 130 131 132 133 |
# File 'lib/parse/model/classes/session.rb', line 127 def revoke_all_for_user(user, except: nil) sessions = for_user(user) sessions = sessions.where(:session_token.ne => except) if except sessions_to_revoke = sessions.all sessions_to_revoke.each(&:destroy) sessions_to_revoke.count end |
.session(token, **opts) ⇒ Session
Return the Session record for this session token.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/parse/model/classes/session.rb', line 79 def session(token, **opts) # A stray :session_token in opts would be forwarded into the request # stack and silently override the positional token argument. Drop it # so the explicit token always wins. opts.delete(:session_token) response = client.fetch_session(token, **opts) if response.success? return Parse::Session.build response.result end nil end |
Instance Method Details
#expired? ⇒ Boolean
Check if this session has expired.
155 156 157 158 |
# File 'lib/parse/model/classes/session.rb', line 155 def expired? return false if expires_at.nil? expires_at < Time.now end |
#expires_within?(duration) ⇒ Boolean
Check if this session expires within the given duration.
188 189 190 191 |
# File 'lib/parse/model/classes/session.rb', line 188 def expires_within?(duration) return false if expires_at.nil? expires_at < (Time.now + duration) end |
#revoke! ⇒ Boolean
Revoke (delete) this session, effectively logging out the user on this device.
197 198 199 |
# File 'lib/parse/model/classes/session.rb', line 197 def revoke! destroy end |
#time_remaining ⇒ Float?
Get the remaining time until this session expires.
175 176 177 178 179 |
# File 'lib/parse/model/classes/session.rb', line 175 def time_remaining return nil if expires_at.nil? remaining = expires_at.to_time - Time.now remaining > 0 ? remaining : 0 end |
#valid? ⇒ Boolean
Check if this session is still valid (not expired).
166 167 168 |
# File 'lib/parse/model/classes/session.rb', line 166 def valid? !expired? end |