React

Next.js 14 Middleware๋ฅผ ํ™œ์šฉํ•œ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ๋ฆฌ๋‹ค์ด๋ ‰์…˜ ๋ฐฉ๋ฒ•

martinooo 2024. 10. 15. 19:21

Next.js

 

 

 

 

 

๐Ÿคท‍โ™‚๏ธ ๋ณธ๋ก ์— ์•ž์„œ ๋ฏธ๋“ค์›จ์–ด ๊ธฐ๋ณธ๊ฐœ๋…


  • ๋ฏธ๋“ค์›จ์–ด๋Š” ์„œ๋ฒ„ ์ธก์—์„œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์ „์— ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜์ด๋ฉฐ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ํ†ตํ•ด์„œ ์š”์ฒญ์—๋Œ€ํ•œ ์ž‘์—…์„ ์‚ฌ์ „์— ์ˆ˜ํ–‰ํ• ์ˆ˜ ์žˆ๋„๋ก ๋•์Šต๋‹ˆ๋‹ค.
  • ๋ฏธ๋“ค์›จ์–ด ํŒŒ์ผ์€ middleware.ts ๋˜๋Š” middleware.js ํŒŒ์ผ๋ช…์œผ๋กœ ./pages, ./app ํด๋”์™€ ๊ฐ™์€ ๊ณ„์ธต์— ์œ„์น˜ํ•ด์•ผ๋ฉ๋‹ˆ๋‹ค.

 

Next.js ๋ฒ„์ „ ๋งˆ๋‹ค ๋ฃจํŠธ ๊ฒฝ๋กœ์— /pages, /app ๋‹ค๋ฅผ์ˆ˜ ์žˆ์ง€๋งŒ /pages, /app ํด๋”์™€ ๊ฐ™์€ ๊ณ„์ธต์— ๋ฏธ๋“ค์›จ์–ด ํŒŒ์ผ์„ ์œ„์น˜์‹œํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋ฏธ๋“ค์›จ์–ด ํŒŒ์ผ๋ช…๊ณผ ๊ฒฝ๋กœ ๊ทœ์น™์„ ๊ผญ ์ค€์ˆ˜ํ•˜์…”์•ผ ๋ฏธ๋“ค์›จ์–ด ํ•จ์ˆ˜๊ฐ€ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿคท‍โ™‚๏ธ Middleware.ts ์‚ฌ์šฉ๋ฒ•


๋ฏธ๋“ค์›จ์–ด ์‚ฌ์šฉ๋ฐฉ๋ฒ•

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
 
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
  return NextResponse.redirect(new URL('/home', request.url))
}
 
// matcher์— ๋งค์นญ๋˜๋Š” ๊ฒฝ๋กœ๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ middleware.ts ์‹คํ–‰
export const config = {
  matcher: '/about/:path*',
}

 

matcher ์„ค์ •

ํŠน์ • ๊ฒฝ๋กœ ์„ค์ •

export const config = {
  matcher: '/about/:path*',
}

 

๋ฐฐ์—ด ๊ตฌ๋ฌธ์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฒฝ๋กœ ์„ค์ •

export const config = {
  matcher: ['/about/:path*', '/dashboard/:path*'],
}

 

matcher์— ๊ฒฝ๋กœ ์„ค์ •์œผ๋กœ middleware๋ฅผ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Next.js middleware ๊ณต์‹๋ฌธ์„œ

https://nextjs.org/docs/app/building-your-application/routing/middleware

 

Routing: Middleware | Next.js

Learn how to use Middleware to run code before a request is completed.

nextjs.org

 

 

 

๐Ÿคท‍โ™‚๏ธ Middleware๋ฅผ ํ™œ์šฉํ•œ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ๋ฆฌ๋‹ค์ด๋ ‰์…˜ ๋ฐฉ๋ฒ•


์‹ค์ œ ์ œ๊ฐ€ ์šด์˜์ค‘์ธ ์‡ผํ•‘๋ชฐ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

 

Next.js์˜ middleware ํ•จ์ˆ˜์—์„œ ์š”์ฒญ ํ—ค๋”์˜ ์ฟ ํ‚ค์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ token์„ ํ™œ์šฉํ•ด์„œ ๋กœ๊ทธ์ธ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜์—ฌ Redirection์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import { getToken } from 'next-auth/jwt';
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

// ๋ฏธ๋“ค์›จ์–ด์—์„œ ์‚ฌ์šฉ๋  secret ์„ค์ •
const secret = process.env.AUTH_SECRET;

export async function middleware(req: NextRequest) {
  // ์„ธ์…˜ ํ† ํฐ ํ™•์ธ (JWT ์‚ฌ์šฉ)
  const token = await getToken({ req, secret });

  // ์„ธ์…˜์ด ์—†์œผ๋ฉด ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ
  if (!token) {
    const loginUrl = new URL('/login', req.url);
    return NextResponse.redirect(loginUrl);
  }

  // ์„ธ์…˜์ด ์žˆ์œผ๋ฉด ์š”์ฒญ์„ ์ •์ƒ ์ฒ˜๋ฆฌ
  return NextResponse.next();
}

// ์ด ๋ฏธ๋“ค์›จ์–ด๊ฐ€ ์ ์šฉ๋  ๊ฒฝ๋กœ (๋ณดํ˜ธ๋œ ๊ฒฝ๋กœ ์„ค์ •)
export const config = {
  matcher: [
    '/cart',
    '/cart/:path*',
    '/profile',
    '/profile/:path*',
    '/purchase-history',
    '/purchase-history/:path*',
    '/checkout',
    '/checkout/:path*'
  ] // ๋ณดํ˜ธํ•  ๊ฒฝ๋กœ๋ฅผ ์„ค์ • (์˜ˆ: /protected/*)
};

 

token์ด ์—†์œผ๋ฉด ๋กœ๊ทธ์ธํ•œ ์œ ์ €๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ Redirection ์‹œํ‚ต๋‹ˆ๋‹ค.

 

Middleware ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

  • ์„ฑ๋Šฅ ํ–ฅ์ƒ: ์„œ๋ฒ„ ์ธก์—์„œ ์š”์ฒญ์„ ๋ฏธ๋ฆฌ ์ฒ˜๋ฆฌํ•จ์œผ๋กœ์จ ํด๋ผ์ด์–ธํŠธ์˜ ๋ถ€ํ•˜๋ฅผ ์ค„์ด๊ณ , ๋” ๋น ๋ฅธ ์‘๋‹ต์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์œ ์—ฐ์„ฑ: ๋‹ค์–‘ํ•œ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด, ๋ณต์žกํ•œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ณด์•ˆ ๊ฐ•ํ™”: ์ธ์ฆ ๋ฐ ๊ถŒํ•œ ๊ด€๋ฆฌ๋ฅผ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ณด์•ˆ์„ ๊ฐ•ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์žฌ๋ฐŒ๋Š” ์˜ค๋Š˜์˜ ๋ ๋ณ„ ์šด์„ธ ๋ณด๊ณ  ๊ฐ€์„ธ์š”! 

 

 

https://cometruedream.tistory.com/247

 

[React] Tailwind ํ•œ์ค„, ๋‘์ค„, ์„ธ์ค„ ํšจ๊ณผ className ์ปค์Šคํ…€

๐Ÿคท‍โ™‚๏ธ Tailwind ํ•œ์ค„, ๋‘์ค„, ์„ธ์ค„ ํšจ๊ณผํ”„๋กœ์ ํŠธ ์ง„ํ–‰์ค‘ ๋ฐฐ๋„ˆ ๋งํฌ์— URL ๊ธธ์ด๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด์„œ 3์ค„ ํšจ๊ณผ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. React + Tailwind CSS ์…‹ํŒ…๋œ ํ”„๋กœ์ ํŠธ ์—ฌ์„œ CSS ํŒŒ์ผ & style ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜

cometruedream.tistory.com

 

 

https://cometruedream.tistory.com/243

 

NestJS Lifecycle (์ƒ๋ช…์ฃผ๊ธฐ) ๊ฐœ๋…

๐Ÿคท‍โ™‚๏ธ ๋ณธ๋ก ์— ์•ž์„œ Nest JS ๊ฐœ๋…?Nest JS๋Š” Node JS ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์„œ๋ฒ„ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋ ˆ์ž„์›Œํฌ ์ž…๋‹ˆ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์ƒ๋‹นํžˆ ์˜ค๋žซ๋™์•ˆ Express ๊ธฐ๋ฐ˜์œผ๋กœ ์„œ๋ฒ„ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์—์„œ ์••๋„์ ์ธ ์ 

cometruedream.tistory.com

 

 

https://cometruedream.tistory.com/242

 

๋ฆฌ์•กํŠธ Vite Proxy ํ™œ์šฉํ•œ CORS ์˜ค๋ฅ˜ ํ•ด๊ฒฐ

๐Ÿคท‍โ™‚๏ธ ๋ณธ๋ก ์— ์•ž์„œํ”„๋ก ํŠธ์—”๋“œ ๋ฒˆ๋“ค๋Ÿฌ ๊ฐœ๋…๊ณผ webpack vs vite ๋น„๊ต์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ๊ธ€์„ ๋ณด์‹œ๋ฉด ๋” ๋งŽ์€ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค^^ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ๋ฒˆ๋“ค๋Ÿฌ ๊ฐœ๋…๊ณผ webpack vs vite ๋น„๊ตhttps://comet

cometruedream.tistory.com

 

 

https://cometruedream.tistory.com/241

 

React ํด๋”๊ตฌ์กฐ FSD ๊ธฐ๋Šฅ ๋ถ„ํ™œ ์„ค๊ณ„ ์•„ํ‚คํ…์ฒ˜

๐Ÿคท‍โ™‚๏ธ ๋ณธ๋ก ์— ์•ž์„œํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ ํด๋” ๊ตฌ์กฐ, ์„ค๊ณ„์— ๋Œ€ํ•œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ์ƒ๋‹นํžˆ ์–ด๋ ต๋‹ค. ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์— React FSD ํด๋”๊ตฌ์กฐ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์„ค๊ณ„ํ•  ์˜ˆ์ •์ด๋ผ์„œ FSD ์•„ํ‚คํ…

cometruedream.tistory.com

 

 

https://cometruedream.tistory.com/244

 

์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋Š” Prisma ๋ช…๋ น์–ด ์ •๋ฆฌ

๐Ÿคท‍โ™‚๏ธ Prisma ๋ช…๋ น์–ด ๋ณธ๋ก ์— ์•ž์„œ Prisma๋ž€?Nest JS ํ”„๋ ˆ์ž„์›Œํฌ์™€ Prisma ORM์„ ํ™œ์šฉํ•ด์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ Prisma๋Š” Node JS ๊ธฐ๋ฐ˜์— ํ”„๋ ˆ์ž„์›Œํฌ/๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ์ƒํ˜ธ์ž‘์šฉ์ด ์ž˜๋˜๋ฉฐ Prisma + DB ์—ฐ๋™์œผ

cometruedream.tistory.com