I have installed NestJS v.10.3.9, Fastify v.4.28.0, @fastify/secure-session v7.5.1 and @fastify/redis v6.2.0 because I need pernament session to save tokens (id tokens/refresh tokens) to protect authentication against abuse and actions related to other users accounts.
I tried using the code below, but I kept getting a type error, which prevented me from compiling the Nest application.
Code:
<code>import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
} from '@nestjs/platform-fastify';
import secureSession from '@fastify/secure-session';
import fastifyRedis from '@fastify/redis';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
const configService: ConfigService = app.get(ConfigService);
const fastifyInstance = app.getHttpAdapter().getInstance();
.addHook('onRequest', async (req, res) => {
req.socket['encrypted'] =
configService.get<string>('NODE_ENV', '') === 'production';
res.header('X-Powered-By', 'CyberSecurity');
.decorateReply('setHeader', (name: string, value: unknown) => {
this.header(name, value);
.decorateReply('end', () => {
const port = configService.get<string>('PORT', '');
fastifyRedis as Parameters<NestFastifyApplication['register']>[0],
host: configService.get<string>('REDIS_HOST', ''),
port: configService.get<string>('REDIS_PORT', ''),
password: configService.get<string>('REDIS_PASSWORD', ''),
secureSession as Parameters<NestFastifyApplication['register']>[0],
sessionName: configService.get<string>('SESSION_NAME', ''),
cookieName: configService.get<string>('SESSION_COOKIE', ''),
expiry: configService.get<string>('SESSION_EXPIRATION_TIME', ''),
secure: configService.get<string>('NODE_ENV') === 'production',
secret: configService.get<string>('SESSION_SECRET', ''),
salt: configService.get<string>('SESSION_SALT', ''),
<code>import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import secureSession from '@fastify/secure-session';
import fastifyRedis from '@fastify/redis';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
);
const configService: ConfigService = app.get(ConfigService);
const fastifyInstance = app.getHttpAdapter().getInstance();
fastifyInstance
.addHook('onRequest', async (req, res) => {
req.socket['encrypted'] =
configService.get<string>('NODE_ENV', '') === 'production';
res.header('X-Powered-By', 'CyberSecurity');
})
.decorateReply('setHeader', (name: string, value: unknown) => {
this.header(name, value);
})
.decorateReply('end', () => {
this.send('');
});
const port = configService.get<string>('PORT', '');
await app.register(
fastifyRedis as Parameters<NestFastifyApplication['register']>[0],
{
host: configService.get<string>('REDIS_HOST', ''),
port: configService.get<string>('REDIS_PORT', ''),
password: configService.get<string>('REDIS_PASSWORD', ''),
},
);
await app.register(
secureSession as Parameters<NestFastifyApplication['register']>[0],
{
sessionName: configService.get<string>('SESSION_NAME', ''),
cookieName: configService.get<string>('SESSION_COOKIE', ''),
expiry: configService.get<string>('SESSION_EXPIRATION_TIME', ''),
cookie: {
path: '/',
httpOnly: true,
secure: configService.get<string>('NODE_ENV') === 'production',
},
secret: configService.get<string>('SESSION_SECRET', ''),
salt: configService.get<string>('SESSION_SALT', ''),
store: {
type: 'redis',
options: {
client: app.redis,
},
},
},
);
await app.listen(port);
}
bootstrap();
</code>
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import secureSession from '@fastify/secure-session';
import fastifyRedis from '@fastify/redis';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
);
const configService: ConfigService = app.get(ConfigService);
const fastifyInstance = app.getHttpAdapter().getInstance();
fastifyInstance
.addHook('onRequest', async (req, res) => {
req.socket['encrypted'] =
configService.get<string>('NODE_ENV', '') === 'production';
res.header('X-Powered-By', 'CyberSecurity');
})
.decorateReply('setHeader', (name: string, value: unknown) => {
this.header(name, value);
})
.decorateReply('end', () => {
this.send('');
});
const port = configService.get<string>('PORT', '');
await app.register(
fastifyRedis as Parameters<NestFastifyApplication['register']>[0],
{
host: configService.get<string>('REDIS_HOST', ''),
port: configService.get<string>('REDIS_PORT', ''),
password: configService.get<string>('REDIS_PASSWORD', ''),
},
);
await app.register(
secureSession as Parameters<NestFastifyApplication['register']>[0],
{
sessionName: configService.get<string>('SESSION_NAME', ''),
cookieName: configService.get<string>('SESSION_COOKIE', ''),
expiry: configService.get<string>('SESSION_EXPIRATION_TIME', ''),
cookie: {
path: '/',
httpOnly: true,
secure: configService.get<string>('NODE_ENV') === 'production',
},
secret: configService.get<string>('SESSION_SECRET', ''),
salt: configService.get<string>('SESSION_SALT', ''),
store: {
type: 'redis',
options: {
client: app.redis,
},
},
},
);
await app.listen(port);
}
bootstrap();
I expected that after configuring Redis and Sessions, the application would compile normally and create a session for me.
Error:
<code>src/main.ts:89:23 - error TS2339: Property 'redis' does not exist on type 'NestFastifyApplication<RawServerDefault>'.
<code>src/main.ts:89:23 - error TS2339: Property 'redis' does not exist on type 'NestFastifyApplication<RawServerDefault>'.
89 client: app.redis,
</code>
src/main.ts:89:23 - error TS2339: Property 'redis' does not exist on type 'NestFastifyApplication<RawServerDefault>'.
89 client: app.redis,