ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ช NestJS Cookies – ์ค์ ์ฟ ํค ๊ด๋ฆฌ์ ๋ณด์๊น์ง ์๋ฒฝ ๊ฐ์ด๋
octo54 2025. 6. 10. 13:44๐ช NestJS Cookies – ์ค์ ์ฟ ํค ๊ด๋ฆฌ์ ๋ณด์๊น์ง ์๋ฒฝ ๊ฐ์ด๋
NestJS๋ Express ๋๋ Fastify ํ์ ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์,
์ฟ ํค๋ฅผ ์ฝ๊ฒ ์ฝ๊ณ ์ฐ๊ธฐ ์ํ ๋ฏธ๋ค์จ์ด ์ค์ ๋ฐ ๋ฐ์ฝ๋ ์ดํฐ ๊ธฐ๋ฐ ์ ๊ทผ์ ์ง์ํฉ๋๋ค.
์ด๋ฒ ๊ธ์ NestJS ๊ณต์ ๋ฌธ์ – Cookies๋ฅผ ๋ฐํ์ผ๋ก,
์ฟ ํค์ ์ฝ๊ธฐ/์ฐ๊ธฐ/์ญ์ ๋ถํฐ ์๋ช
์ฟ ํค, ๋ณด์ ์ต์
, ์ค๋ฌด ์ ์ฉ๋ฒ๊น์ง ์์ธํ ์ค๋ช
ํฉ๋๋ค.
โ 1. cookie-parser ์ค์น ๋ฐ ๋ฑ๋ก (Express ๊ธฐ์ค)
npm install cookie-parser
// main.ts
import * as cookieParser from 'cookie-parser';
const app = await NestFactory.create(AppModule);
app.use(cookieParser('your-secret-key')); // ์๋ช
์ฟ ํค์ฉ ํค
await app.listen(3000);
โ๏ธ cookieParser()๋ฅผ use()๋ก ๋ฑ๋กํด์ผ ๋ชจ๋ ์์ฒญ์ ์ฟ ํค ์ ๊ทผ ๊ฐ๋ฅ
โ๏ธ secret ํค๋ฅผ ์ง์ ํ๋ฉด signed ์ฟ ํค ๊ธฐ๋ฅ ์ฌ์ฉ ๊ฐ๋ฅ
โ 2. ์ฟ ํค ์ฝ๊ธฐ – @Req() ๋๋ FastifyRequest
@Get('read')
readCookie(@Req() request: Request) {
return request.cookies['myCookie']; // ์ผ๋ฐ ์ฟ ํค
}
โ๏ธ request.cookies, request.signedCookies ๋ก ์ ๊ทผ ๊ฐ๋ฅ
โ 3. ์ฟ ํค ์ฐ๊ธฐ – @Res() ์ฌ์ฉ
@Post('set')
setCookie(@Res() response: Response) {
response.cookie('myCookie', 'hello', {
httpOnly: true,
maxAge: 60000, // 1๋ถ
});
response.send('์ฟ ํค ์ค์ ์๋ฃ');
}
์ฃผ์ ์ต์
์ต์ ์ค๋ช
httpOnly | JS ์ ๊ทผ ์ฐจ๋จ (๋ณด์ ํ์) |
secure | HTTPS ํ๊ฒฝ์์๋ง ์ ์ก |
signed | ์๋ช ๋ ์ฟ ํค (๋ณ์กฐ ๊ฐ์ง) |
maxAge | ์ฟ ํค ์ ํจ ์๊ฐ (๋ฐ๋ฆฌ์ด) |
sameSite | CSRF ๋ฐฉ์ง์ฉ ์ ์ฑ (strict, lax, none) |
โ 4. ์๋ช ๋ ์ฟ ํค (signed cookies)
@Res() response.cookie('signedCookie', 'secretValue', {
signed: true,
});
@Req() request.signedCookies['signedCookie'];
โ๏ธ ํด๋ผ์ด์ธํธ๊ฐ ๊ฐ์ ์กฐ์ํ์ง ๋ชปํ๋๋ก ์๋ช ๋ ์ฟ ํค ์ฌ์ฉ์ ๊ถ์ฅ
โ๏ธ ๋จ, cookieParser('secret') ํ์
โ 5. ์ฟ ํค ์ญ์
@Delete('clear')
clearCookie(@Res() response: Response) {
response.clearCookie('myCookie');
response.send('์ฟ ํค ์ญ์ ๋จ');
}
โ 6. Fastify ์ฌ์ฉ ์ ์ฐธ๊ณ ์ฌํญ
- Fastify์์๋ fastify-cookie ํ๋ฌ๊ทธ์ธ์ด ํ์ํฉ๋๋ค:
npm install fastify-cookie
import fastifyCookie from '@fastify/cookie';
app.register(fastifyCookie, {
secret: 'my-secret', // ์๋ช
์ฟ ํค์ฉ
});
โ๏ธ Express์ ๋์ผํ๊ฒ request.cookies, response.setCookie() ํ์ฉ ๊ฐ๋ฅ
๐ง ์ค๋ฌด ์ ๋ต ์์ฝ
์์ ๋ฐฉ๋ฒ
์ฟ ํค ์ฝ๊ธฐ | request.cookies['key'] |
์ฟ ํค ์ค์ | response.cookie('key', 'value', options) |
์ฟ ํค ์ญ์ | response.clearCookie('key') |
์๋ช ์ฟ ํค ์ฌ์ฉ | signed: true + cookie-parser(secret) |
๋ณด์ ์ค์ | httpOnly, secure, sameSite ์ค์ ๊ถ์ฅ |
NestJS ์ฟ ํค ์ฌ์ฉ๋ฒ,NestJS cookie-parser,NestJS signed cookies,NestJS ์ฟ ํค ๋ณด์,NestJS ์ฟ ํค ์ค์ ,NestJS Fastify ์ฟ ํค,NestJS httpOnly ์ฟ ํค,NestJS response.cookie,NestJS request.cookies,NestJS ์ค๋ฌด ์ฟ ํค ๊ด๋ฆฌ
'framework > NestJS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- Total
- Today
- Yesterday
- nodejs
- fastapi
- ๋ฅ๋ฌ๋
- Next.js
- App Router
- NestJS
- ํ์ด์ฌ์๊ณ ๋ฆฌ์ฆ
- PostgreSQL
- seo ์ต์ ํ 10๊ฐ
- REACT
- ์น๊ฐ๋ฐ
- SEO ์ต์ ํ
- SEO์ต์ ํ
- ํ๋ก ํธ์๋๋ฉด์
- ๊ฐ๋ฐ๋ธ๋ก๊ทธ
- nextJS
- ํ๋ก ํธ์๋
- ๋ฐฑ์๋๊ฐ๋ฐ
- rag
- AI์ฑ๋ด
- JAX
- CI/CD
- kotlin
- Prisma
- Python
- Docker
- gatsbyjs
- Ktor
- flax
- llm
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |