Skip to content

Commit

Permalink
Revert "move setup sessions to server config"
Browse files Browse the repository at this point in the history
This reverts commit 86b176d.
  • Loading branch information
sszafGCA committed Nov 23, 2023
1 parent 235a83c commit e4b133d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 48 deletions.
4 changes: 2 additions & 2 deletions apps/server/src/modules/server/admin-api.server.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { FileEntity } from '@modules/files/entity';
import { FileRecord } from '@modules/files-storage/entity';
import { RedisClient } from 'redis';
import { REDIS_CLIENT, RedisModule } from '@src/infra/redis';
import { defaultMikroOrmOptions } from './server.module';
import { serverConfig, setupSessions } from './server.config';
import { defaultMikroOrmOptions, setupSessions } from './server.module';
import { serverConfig } from './server.config';
import { AdminApiServerController } from './controller';
import { DeletionApiModule } from '../deletion/deletion-api.module';

Expand Down
45 changes: 0 additions & 45 deletions apps/server/src/modules/server/server.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import type { CommonCartridgeConfig } from '@modules/learnroom/common-cartridge'
import type { UserConfig } from '@modules/user';
import type { CoreModuleConfig } from '@src/core';
import { MailConfig } from '@src/infra/mail/interfaces/mail-config';
import { MiddlewareConsumer } from '@nestjs/common';
import { RedisClient } from 'redis';
import { LegacyLogger } from '@src/core/logger';
import connectRedis from 'connect-redis';
import session from 'express-session';
import { XApiKeyConfig } from '../authentication/config/x-api-key.config';

export enum NodeEnvType {
Expand Down Expand Up @@ -57,43 +52,3 @@ const config: ServerConfig = {
};

export const serverConfig = () => config;

export const setupSessions = (
consumer: MiddlewareConsumer,
redisClient: RedisClient | undefined,
logger: LegacyLogger
) => {
const sessionDuration: number = Configuration.get('SESSION__EXPIRES_SECONDS') as number;

let store: connectRedis.RedisStore | undefined;
if (redisClient) {
const RedisStore: connectRedis.RedisStore = connectRedis(session);
store = new RedisStore({
client: redisClient,
ttl: sessionDuration,
});
} else {
logger.warn(
'The RedisStore for sessions is not setup, since the environment variable REDIS_URI is not defined. Sessions are using the build-in MemoryStore. This should not be used in production!'
);
}

consumer
.apply(
session({
store,
secret: Configuration.get('SESSION__SECRET') as string,
resave: false,
saveUninitialized: false,
name: Configuration.has('SESSION__NAME') ? (Configuration.get('SESSION__NAME') as string) : undefined,
proxy: Configuration.has('SESSION__PROXY') ? (Configuration.get('SESSION__PROXY') as boolean) : undefined,
cookie: {
secure: Configuration.get('SESSION__SECURE') as boolean,
sameSite: Configuration.get('SESSION__SAME_SITE') as boolean | 'lax' | 'strict' | 'none',
httpOnly: Configuration.get('SESSION__HTTP_ONLY') as boolean,
maxAge: sessionDuration * 1000,
},
})
)
.forRoutes('*');
};
44 changes: 43 additions & 1 deletion apps/server/src/modules/server/server.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ import { ALL_ENTITIES } from '@shared/domain';
import { createConfigModuleOptions, DB_PASSWORD, DB_URL, DB_USERNAME } from '@src/config';
import { CoreModule } from '@src/core';
import { LegacyLogger, LoggerModule } from '@src/core/logger';
import connectRedis from 'connect-redis';
import session from 'express-session';
import { RedisClient } from 'redis';
import { ServerController } from './controller/server.controller';
import { serverConfig, setupSessions } from './server.config';
import { serverConfig } from './server.config';

const serverModules = [
ConfigModule.forRoot(createConfigModuleOptions(serverConfig)),
Expand Down Expand Up @@ -83,6 +85,46 @@ export const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = {
new NotFoundException(`The requested ${entityName}: ${where} has not been found.`),
};

export const setupSessions = (
consumer: MiddlewareConsumer,
redisClient: RedisClient | undefined,
logger: LegacyLogger
) => {
const sessionDuration: number = Configuration.get('SESSION__EXPIRES_SECONDS') as number;

let store: connectRedis.RedisStore | undefined;
if (redisClient) {
const RedisStore: connectRedis.RedisStore = connectRedis(session);
store = new RedisStore({
client: redisClient,
ttl: sessionDuration,
});
} else {
logger.warn(
'The RedisStore for sessions is not setup, since the environment variable REDIS_URI is not defined. Sessions are using the build-in MemoryStore. This should not be used in production!'
);
}

consumer
.apply(
session({
store,
secret: Configuration.get('SESSION__SECRET') as string,
resave: false,
saveUninitialized: false,
name: Configuration.has('SESSION__NAME') ? (Configuration.get('SESSION__NAME') as string) : undefined,
proxy: Configuration.has('SESSION__PROXY') ? (Configuration.get('SESSION__PROXY') as boolean) : undefined,
cookie: {
secure: Configuration.get('SESSION__SECURE') as boolean,
sameSite: Configuration.get('SESSION__SAME_SITE') as boolean | 'lax' | 'strict' | 'none',
httpOnly: Configuration.get('SESSION__HTTP_ONLY') as boolean,
maxAge: sessionDuration * 1000,
},
})
)
.forRoutes('*');
};

/**
* Server Module used for production
*/
Expand Down

0 comments on commit e4b133d

Please sign in to comment.