Reputation: 1555
Am trying to set property in session but getting undefined. Using NestJs + Fastify
app.module.ts
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
// import fastifyCookie, { FastifyCookieOptions } from 'fastify-cookie'; --this was also not workin
const cookieSession = require( 'cookie-session' );
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter( )
);
app.register( cookieSession( {
keys: [ 'asdsds' ]
} ) );
lines...
}
bootstrap();
users.controller.ts
imports...
export class UsersController {
constructor( private usersService: UsersService, private authService: AuthService ) { }
@Get( '/colors/:color' )
setColor( @Param( 'color' ) color: string, @Session( ) session: any ) {
// console.log( 'check ', session ); --> UNDEFINED
session.color = color; --> Error HERE
// session.set( color );
}
@Get( '/colors' )
getColor( @Session( ) session: any ) {
console.log( 'get session: ', session );
return session.color;
}
Error is:
[Nest] 41229 - 13/02/2022, 9:51:06 pm ERROR [ExceptionsHandler] Cannot set properties of undefined (setting 'color')
TypeError: Cannot set properties of undefined (setting 'color')
at UsersController.setColor (/home/prabhat/Practice/Nest-Fastify/nestfastify/src/users/users.controller.ts:20:22)
at /home/prabhat/Practice/Nest-Fastify/nestfastify/node_modules/@nestjs/core/router/router-execution-context.js:38:29
at processTicksAndRejections (node:internal/process/task_queues:96:5)
I have also tried with
// app.register(fastifyCookie );
// app.register(fastifySession, {secret: 'a secret with minimum length of 32 characters'});
Upvotes: 0
Views: 2658
Reputation: 1555
What worked for me is fastify-secure-session
package, mentioned here
Now my main.ts looks like:
imports..
import secureSession from 'fastify-secure-session';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter( )
);
app.register(secureSession, {
secret: 'averylogphrasebiggerthanthirtytwochars',
salt: 'mq9hDxBVDbspDR6n',
});
lines..
}
bootstrap();
And app.controller.ts is:
imports..
import * as secureSession from 'fastify-secure-session'
@Controller( 'auth' )
@MongooseClassSerializerInterceptor( UserDto )
export class UsersController {
constructor( private usersService: UsersService, private authService: AuthService ) { }
@Get( '/colors/:color' )
setColor( @Param( 'color' ) color: string, @Session( ) session: secureSession.Session ) {
console.log( 'check ', session );
// session.color = color;
session.set('color', color)
}
@Get( '/colors' )
getColor( @Session( ) session: secureSession.Session ) {
console.log( 'get session: ', session );
// return session.color;
return session.get( 'color' );
}
}
With this am able to set cookie, in NestJs with Fastify.
Still interested in knowing why previous approach is not working for me as mentioned by @Jay in their answer.
Upvotes: 1
Reputation: 70412
cookie-session
is an express middleware. so if it's being used with fastify you'll probably end up with req.raw.session
for the cookie.
fastify-cookie
is a fastify specific package for cookie support, but not sessions directly, so you' need to use req.cookie
or make a @Cookie()
decorator
@fastify/session
is the package for session support in fastify. By using @fastify/session
with fastify-cookie
you will have access to both req.session
and req.cookie
Upvotes: 1