Session Storages
Session storages are used by the sessions middleware to persist sessions acrossrequests. Session storages must implement the fromClient
and forClient
methods and can optionally implement the new
method.
The built-in session storages generally provide the following attributes:
- uid:
string
(Default:null
)
A unique identifier indicating the active user.
- created:
number
(Default:Date.now()
)
The numeric timestamp of when the session was created.
- data:
any
(Default:null
)
Arbitrary data to persisted in the session.
new
storage.new(): Session
Generates a new session object representing an empty session.The empty session object should not be persisted unless necessary.The return value will be exposed by the middleware as the session
propertyof the request object if no session identifier was returned by the sessiontransports and auto-creation is not explicitly disabled in the session middleware.
Examples
new() {
return {
uid: null,
created: Date.now(),
data: null
};
}
fromClient
storage.fromClient(sid): Session | null
Resolves or deserializes a session identifier to a session object.
Arguments
- sid:
string
Session identifier to resolve or deserialize.
Returns a session object representing the session with the given sessionidentifier that will be exposed by the middleware as the session
property ofthe request object. This method will only be called if any of the session transportsreturned a session identifier. If the session identifier is invalid or expired,the method should return a null
value to indicate no matching session.
Examples
fromClient(sid) {
return db._collection('sessions').firstExample({_key: sid});
}
forClient
storage.forClient(session): string | null
Derives a session identifier from the given session object.
Arguments
- session:
Session
Session to derive a session identifier from.
Returns a session identifier for the session represented by the givensession object. This method will be called with the session
propertyof the request object unless that property is empty (e.g. null
).
Examples
forClient(session) {
if (!session._key) {
const meta = db._collection('sessions').save(session);
return meta._key;
}
db._collection('sessions').replace(session._key, session);
return session._key;
}