sessionMiddleware function
Middleware
sessionMiddleware()
Returns the session middleware.
Implementation
Middleware sessionMiddleware() {
return (Handler innerHandler) {
return (Request request) async {
request = _addSessionIdToRequest(request);
final sessionId = _getSessionId(request);
final requestedUri = request.requestedUri;
final isSecure = requestedUri.scheme == 'https';
final now = DateTime.now();
final lifetime = Session.lifetime;
final expires = now.add(lifetime);
final cookie = Cookie(
Session.name,
sessionId,
);
cookie.secure = isSecure;
cookie.path = '/';
cookie.maxAge = expires.difference(DateTime.now()).inSeconds;
cookie.expires = expires;
cookie.httpOnly = true;
request.addCookie(cookie);
final response = await innerHandler(request);
final session = _sessions[sessionId];
if (session != null) {
session.expires = expires;
}
return response;
};
};
}