allow undefined arg for cookie, just set cookie if session has entries

This commit is contained in:
HorizonCode 2023-05-13 06:29:01 +02:00
parent 96a73035f8
commit 8a3683d2e5

20
mod.ts
View File

@ -278,13 +278,17 @@ export class HTTPServer {
private processSession(routeRequest: RouteRequest, routeReply: RouteReply) {
if (this.settings?.sessionSecret) {
const sessionObject = JSON.stringify(routeRequest.session);
const encodedSession = encryptData(
sessionObject,
this.settings?.sessionSecret,
);
routeReply.cookie("session", encodedSession, {
maxAge: this.settings.sessionExpire ?? undefined,
});
if (Object.keys(routeRequest.session).length > 0) {
const encodedSession = encryptData(
sessionObject,
this.settings?.sessionSecret,
);
routeReply.cookie("session", encodedSession, {
maxAge: this.settings.sessionExpire ?? undefined,
});
}else{
routeReply.cookie("session", undefined);
}
}
}
@ -503,7 +507,7 @@ export class RouteReply {
return this;
}
cookie(name: string, value: string, attributes?: {
cookie(name: string, value: string | undefined, attributes?: {
expires?: Date | number;
maxAge?: number;
domain?: string;