๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[Authentication in Nest.js] Access Tokens & Refresh Tokens ๋ณธ๋ฌธ
๐ฉ๐ป ๋ฐฑ์๋(Back-End)/Nest js
[Authentication in Nest.js] Access Tokens & Refresh Tokens
์ง์ง์ํ์นด 2023. 6. 21. 19:15728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ Code with Vlad ๋์ ์ ํ๋ธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
=> NestJs JWT - Access Tokens & Refresh Tokens - Ultimate Guide
๐ฅ๏ธ ์ด๊ธฐ ํ๊ฒฝ ์ค์
nest new AuthNest
npm add -D prisma@latest
npm add @prisma/client
npx prisma studio
npx prisma init
โก๏ธ Prisma
: SQL ์ฝ๋๋ฅผ ์ฐ์ง ์๊ณ , js์ฝ๋(typescript๋ ๊ฐ๋ฅ)๋ฅผ ์์ฑํด ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์์ ํ ์ ์๋๋ก ์ฐ๊ฒฐํด์ฃผ๋ ์๋น์ค
์ฐจ์ธ๋ Node.js์ TypeScript ORM!
๊ธฐ์กด ORM์ ๊ฐ์ฒด ์งํฅ ๋ฐฉ์์ผ๋ก ๊ด๊ณํ ๋ฐ์ดํฐ ๋ฒ ์ด์ค ํ ์ด๋ธ์ ๋งคํ
Prisma shcema์ ์ ์ธ์ ์ธ ๋ชจ๋ธ์ ์ ์ํด์ ๋ณต์กํ ๋ชจ๋ธ ์ธ์คํด์ค๋ฅผ ๊ด๋ฆฌํ๋๋ฐ ์์ ํ๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ์
Prisma Client : Node.js์ Typescript๋ฅผ ์ํ ์๋ ์์ฑ๊ณผ ์์ ํ ํ์ ์ฟผ๋ฆฌ ๋น๋
Prisma Migrate : ๋ง์ด๊ทธ๋ ์ด์ ์์คํ
Prisma Studio : ๋ฐ์ดํฐ ๋ฒ ์ด์ค์ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ณ ํธ์งํ ์ ์๋ GUI ํธ์ง๊ธฐ
๐ฅ๏ธ 1) docker ์ค์
โก๏ธ docker-compose.yml
version: '3'
services:
db:
image: postgres:12
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 123
POSTGRES_DB: nestjs
โก๏ธ .env
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="postgresql://postgres:123@localhost:5432/nestjs?schema=public"
docker-compose up
๐ฅ๏ธ 2) prisma ์ค์
: DB GUI tool ์ ๊ฐ์ ๊ธฐ๋ฅ์ ํ๋ studio
npx prisma migrate dev --create-only
npx prisma db push
npx prisma studio
๐ฅ๏ธ 3) auth & prisma module ์ด๊ธฐ ์ค์
npx nest g module auth
npx nest g module prisma
npx nest g service prisma
npx nest g controller auth --no-spec
npx nest g service auth --no-spec
// --no-spec : spec.ts ํ์ผ์ ์์ฑ X
โก๏ธ Lifecycle sequence
OnModuleInit() | ํธ์คํธ ๋ชจ๋์ด ์ด๊ธฐํ๋๋ฉด ํธ์ถ |
OnApplicationBootstrap() | ์์ฉ ํ๋ก๊ทธ๋จ์ด ์์ ํ ์์๋๊ณ ๋ถํธ ์คํธ๋ฉ๋๋ฉด ํธ์ถ |
OnModuleDestroy() | Nest๊ฐ ํธ์คํธ ๋ชจ๋์ ํ๊ดดํ๊ธฐ ์ง์ ์ ์ ๋ฆฌ |
beforeApplicationShutdown() | onModuleDestroy() ํธ๋ค๋ฌ๊ฐ ์๋ฃ๋ ํ์ ํธ์ถ |
OnApplicationShutdown() | ์์คํ ์ ํธ์ ์๋ตํฉ๋๋ค |
๐ฅ๏ธ 4) ์์กด์ฑ ์ฃผ์ (Defendency Injection)
: ์ ์ด์ ์ญ์ (Inversion of Control) ์ ๊ธฐ์ ์ค ํ๋
์ ์ด์ ์ญ์ : ๊ฐ๋ฐ์๊ฐ ์ ์ดํด์ผํ ์์ญ์ ํ๋ ์์ํฌ์๊ฒ ๋ฏฟ๊ณ ๋ง๊ธฐ๋ ๊ฒ
์์กด์ฑ ์ฃผ์ : ๊ฐ๋ฐ์๊ฐ ํ์ํ ์์(ํด๋์ค, ํจ์..)๋ค์ ์ธ๋ถ์์ ์์ฑ์๋ฅผ ํตํด ๋ฃ์ด์ค๋ค
๐ฅ๏ธ 5) DTO ์ค์
โก๏ธ class-transformer & class-validator
: ๋ผ์ฐํฐ๋ ์ปจํธ๋กค๋ฌ์ ๋๋ฌํ๊ธฐ ์ ์ ์์ฒญ์ JSON body๋ฅผ ํด๋์ค์ ์ธ์คํด์ค๋ก ๋ณํํ ๋ค์ ๊ฒ์ฆ
npm add class-validator class-transformer
โก๏ธ bcrypt
npm add bcrypt
npm add -D @types/bcrypt
๐ฅ๏ธ 6) Auth ์ค์
โก๏ธ passport
npm add @nestjs/jwt passport-jwt
npm add -D @types/passport-jwt
npm add @nestjs/passport passport
728x90
๋ฐ์ํ
'๐ฉโ๐ป ๋ฐฑ์๋(Back-End) > Nest js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comments