refactor: move typeorm store into new session module

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2022-06-21 16:16:40 +02:00 committed by David Mehren
parent 14ee7485ad
commit 57365bb727
16 changed files with 204 additions and 49 deletions

View file

@ -4,40 +4,34 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { INestApplication } from '@nestjs/common';
import { getRepositoryToken } from '@nestjs/typeorm';
import { TypeormStore } from 'connect-typeorm';
import session from 'express-session';
import { Repository } from 'typeorm';
import { AuthConfig } from '../config/auth.config';
import { DatabaseType } from '../config/database-type.enum';
import { DatabaseConfig } from '../config/database.config';
import { Session } from '../users/session.entity';
export const HEDGEDOC_SESSION = 'hedgedoc-session';
/**
* Setup the session middleware via the given authConfig.
* Set up the session middleware via the given authConfig.
* @param {INestApplication} app - the nest application to configure the middleware for.
* @param {AuthConfig} authConfig - the authConfig to configure the middleware with.
* @param {DatabaseConfig} dbConfig - the DatabaseConfig to configure the middleware with.
* @param {TypeormStore} typeormStore - the typeormStore to handle session data.
*/
export function setupSessionMiddleware(
app: INestApplication,
authConfig: AuthConfig,
dbConfig: DatabaseConfig,
typeormStore: TypeormStore,
): void {
app.use(
session({
name: 'hedgedoc-session',
name: HEDGEDOC_SESSION,
secret: authConfig.session.secret,
cookie: {
maxAge: authConfig.session.lifetime,
},
resave: false,
saveUninitialized: false,
store: new TypeormStore({
cleanupLimit: 2,
limitSubquery: dbConfig.type !== DatabaseType.MARIADB,
}).connect(app.get<Repository<Session>>(getRepositoryToken(Session))),
store: typeormStore,
}),
);
}