fix: refactor session store

This commit is contained in:
Mohamed Marrouchi 2024-10-26 12:02:39 +01:00
parent acc6fcaa88
commit e745b97e87
4 changed files with 23 additions and 16 deletions

View File

@ -134,12 +134,12 @@ export const config: Config = {
max: 100, // Maximum number of items in cache (defaults to 100)
},
mongo: {
user: process.env.MONGO_USER,
password: process.env.MONGO_PASSWORD,
uri: process.env.MONGO_URI,
dbName: process.env.MONGO_DB,
user: process.env.MONGO_USER || 'dev_only',
password: process.env.MONGO_PASSWORD || 'dev_only',
uri: process.env.MONGO_URI || 'mongodb://dev_only:dev_only@mongo:27017/',
dbName: process.env.MONGO_DB || 'hexabot',
},
env: process.env.NODE_ENV,
env: process.env.NODE_ENV || 'development',
authentication: {
jwtOptions: {
salt: parseInt(process.env.SALT_LENGTH || '12'),

View File

@ -23,7 +23,7 @@ import { config } from './config';
import { LoggerService } from './logger/logger.service';
import { seedDatabase } from './seeder';
import { swagger } from './swagger';
import { sessionStore } from './utils/constants/session-store';
import { getSessionStore } from './utils/constants/session-store';
import { ObjectIdPipe } from './utils/pipes/object-id.pipe';
async function bootstrap() {
@ -63,7 +63,7 @@ async function bootstrap() {
proxy: config.security.trustProxy,
resave: true,
saveUninitialized: false,
store: sessionStore,
store: getSessionStore(),
cookie: {
httpOnly: true,
secure: config.security.httpsEnabled,

View File

@ -10,8 +10,15 @@ import MongoStore from 'connect-mongo';
import { config } from '@/config';
export const sessionStore = MongoStore.create({
mongoUrl: config.mongo.uri,
dbName: config.mongo.dbName,
collectionName: 'sessions',
});
let sessionStore: MongoStore = null;
export const getSessionStore = () => {
if (!sessionStore) {
sessionStore = MongoStore.create({
mongoUrl: config.mongo.uri,
dbName: config.mongo.dbName,
collectionName: 'sessions',
});
}
return sessionStore;
};

View File

@ -33,7 +33,7 @@ import {
import { OutgoingMessage, StdEventType } from '@/chat/schemas/types/message';
import { config } from '@/config';
import { LoggerService } from '@/logger/logger.service';
import { sessionStore } from '@/utils/constants/session-store';
import { getSessionStore } from '@/utils/constants/session-store';
import { IOIncomingMessage, IOMessagePipe } from './pipes/io-message.pipe';
import { SocketEventDispatcherService } from './services/socket-event-dispatcher.service';
@ -134,7 +134,7 @@ export class WebsocketGateway
},
passport: { user: {} },
}; // Initialize your session object as needed
sessionStore.set(sid, newSession, (err) => {
getSessionStore().set(sid, newSession, (err) => {
if (err) {
this.logger.error('Error saving session:', err);
return next(new Error('Unable to establish a new socket session'));
@ -179,7 +179,7 @@ export class WebsocketGateway
);
return;
}
sessionStore.set(sessionID, session, (err) => {
getSessionStore().set(sessionID, session, (err) => {
if (err) {
this.logger.error(
'Error saving session in `config.sockets.afterDisconnect`:',
@ -195,7 +195,7 @@ export class WebsocketGateway
sessionID: string,
next: (err: Error, session: any) => void,
): void {
sessionStore.get(sessionID, (err, session) => {
getSessionStore().get(sessionID, (err, session) => {
this.logger.verbose('Retrieved socket session', err || session);
return next(err, session);
});