add sessionExpire option

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

View File

@ -1,6 +1,6 @@
import { Status } from "https://deno.land/std@0.186.0/http/http_status.ts"; import { Status } from "https://deno.land/std@0.186.0/http/http_status.ts";
import prettyTime from "npm:pretty-time"; import prettyTime from "npm:pretty-time";
import { HTTPServer } from "../mod.ts"; import { HTTPServer, SessionExpire } from "../mod.ts";
const JOKES = [ const JOKES = [
"Why do Java developers often wear glasses? They can't C#.", "Why do Java developers often wear glasses? They can't C#.",
@ -194,4 +194,5 @@ httpServer.listen({
staticLocalDir: "/static", staticLocalDir: "/static",
staticServePath: "/assets", staticServePath: "/assets",
sessionSecret: "SuperDuperSecret", sessionSecret: "SuperDuperSecret",
sessionExpire: SessionExpire.NEVER
}); });

13
mod.ts
View File

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