From b7f79a49fd03cb6df0f416ff27feebf99e4d8aee Mon Sep 17 00:00:00 2001 From: Josh Moore Date: Sat, 14 Oct 2023 16:02:19 -0600 Subject: [PATCH] feat: added session types & set up session storage --- backend/app.ts | 15 +++++++++++++++ common/global.d.ts | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/backend/app.ts b/backend/app.ts index e08eb66..857bae3 100644 --- a/backend/app.ts +++ b/backend/app.ts @@ -1,4 +1,6 @@ import express, { Request, Response, NextFunction, RequestHandler, json as BodyParserJson } from 'express'; +import session from 'express-session'; +import MemoryStore from 'memorystore'; import fs from 'fs-extra'; import { path, isProd } from '@tycrek/joint'; import { epcss } from '@tycrek/express-postcss'; @@ -83,9 +85,22 @@ async function main() { // Set up Express const app = express(); + // Configure sessions + const DAY = 86_400_000; + app.use(session({ + name: 'ass', + resave: true, + saveUninitialized: false, + cookie: { maxAge: DAY, secure: isProd() }, + secret: (Math.random() * 100).toString(), + store: new (MemoryStore(session))({ checkPeriod: DAY }) as any, + })); + + // Configure Express features app.enable('case sensitive routing'); app.disable('x-powered-by'); + // Set Express variables app.set('trust proxy', serverConfig.proxied); app.set('view engine', 'pug'); app.set('views', 'views/'); diff --git a/common/global.d.ts b/common/global.d.ts index 124845e..f14f79a 100644 --- a/common/global.d.ts +++ b/common/global.d.ts @@ -1,6 +1,13 @@ import { BusBoyFile } from 'ass'; import { Request, Response } from 'express'; +declare module 'express-session' { + interface SessionData { + uid: string; + token: string; + } +} + declare global { namespace Express { interface Request {