add sessionExpire option

This commit is contained in:
2023-05-13 04:58:01 +02:00
parent d519c7bf9c
commit 5392b032d0
2 changed files with 12 additions and 4 deletions

13
mod.ts
View File

@@ -13,8 +13,13 @@ type HTTPServerOptions = {
staticLocalDir?: string;
staticServePath?: string;
sessionSecret?: string;
sessionExpire?: SessionExpire | number;
};
export enum SessionExpire {
NEVER = 2147483647,
}
export enum HTTPMethod {
GET = "GET",
POST = "POST",
@@ -277,7 +282,9 @@ export class HTTPServer {
sessionObject,
this.settings?.sessionSecret,
);
routeReply.cookie("session", encodedSession);
routeReply.cookie("session", encodedSession, {
maxAge: this.settings.sessionExpire ?? undefined,
});
}
}
@@ -433,9 +440,9 @@ export class RouteRequest {
sessionCookie,
httpServer.settings.sessionSecret,
);
try{
try {
this.session = JSON.parse(decodedSessionCookie);
}catch(_err){
} catch (_err) {
// Ignore if sessionCookie is not in JSON format
}
}