diff --git a/content/techniques/cookies.md b/content/techniques/cookies.md index 8d203c6e5a..759c0a378f 100644 --- a/content/techniques/cookies.md +++ b/content/techniques/cookies.md @@ -26,7 +26,7 @@ You can pass several options to the `cookieParser` middleware: The middleware will parse the `Cookie` header on the request and expose the cookie data as the property `req.cookies` and, if a secret was provided, as the property `req.signedCookies`. These properties are name value pairs of the cookie name to cookie value. -When secret is provided, this module will unsign and validate any signed cookie values and move those name value pairs from `req.cookies` into `req.signedCookies`. A signed cookie is a cookie that has a value prefixed with `s:`. Signed cookies that fail signature validation will have the value `false` instead of the tampered value. +When a secret is provided, this module will unsign and validate any signed cookie values and move those name value pairs from `req.cookies` into `req.signedCookies`. A signed cookie is a cookie that has a value prefixed with `s:`. Signed cookies that fail signature validation will have the value `false` instead of the tampered value. With this in place, you can now read cookies from within the route handlers, as follows: @@ -67,10 +67,7 @@ Once the installation is complete, register the `@fastify/cookie` plugin: import fastifyCookie from '@fastify/cookie'; // somewhere in your initialization file -const app = await NestFactory.create( - AppModule, - new FastifyAdapter(), -); +const app = await NestFactory.create(AppModule, new FastifyAdapter()); await app.register(fastifyCookie, { secret: 'my-secret', // for cookies signature }); @@ -109,12 +106,10 @@ To provide a convenient, declarative way of accessing incoming cookies, we can c ```typescript import { createParamDecorator, ExecutionContext } from '@nestjs/common'; -export const Cookies = createParamDecorator( - (data: string, ctx: ExecutionContext) => { - const request = ctx.switchToHttp().getRequest(); - return data ? request.cookies?.[data] : request.cookies; - }, -); +export const Cookies = createParamDecorator((data: string, ctx: ExecutionContext) => { + const request = ctx.switchToHttp().getRequest(); + return data ? request.cookies?.[data] : request.cookies; +}); ``` The `@Cookies()` decorator will extract all cookies, or a named cookie from the `req.cookies` object and populate the decorated parameter with that value.