From 8a3683d2e5a1b50fc095badde51b32250389b9ee Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Sat, 13 May 2023 06:29:01 +0200 Subject: [PATCH] allow undefined arg for cookie, just set cookie if session has entries --- mod.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/mod.ts b/mod.ts index 544cb13..055aeef 100644 --- a/mod.ts +++ b/mod.ts @@ -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;