read method
Reads an existing session or creates a new one if it does not exist.
This method retrieves a session associated with the given request and
session name. If a session exists, it's loaded; otherwise, a new session
is created.
The request object provides context, including cookies or headers used to
identify the session. The name parameter uniquely identifies the session.
Returns a Future<Session> that completes with the loaded or newly created
Session object.
Example:
final session = await store.read(request, 'my_session');
Implementation
@override
Future<Session> read(Request request, String name) async {
final cookie = _resolveCookie(request, name);
final options = _cloneOptions();
_purgeExpired();
if (cookie.value.isEmpty) {
return Session(name: name, options: options);
}
final sessionId = _decodeSessionId(cookie, name);
if (sessionId == null) {
return Session(name: name, options: options);
}
final stored = _sessions[sessionId];
if (stored == null) {
final session = Session(name: name, options: options)..id = sessionId;
session.isNew = false;
return session;
}
try {
final session = Session.deserialize(stored.payload)..isNew = false;
return session;
} catch (_) {
final session = Session(name: name, options: options)..id = sessionId;
session.isNew = false;
return session;
}
}