NextAuth

ํ•™์Šตํ‚ค์›Œ๋“œ

  • ์„ค์น˜ ๋ฐ ์„ธํŒ…

Next Auth

์„ค์น˜ ๋ฐ ์„ธํŒ…

Next14 ๋ฒ„์ „์€ ํ˜„์žฌ ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ ์žˆ์Œ (24.06.13 ๊ธฐ์ค€)

npm install next-auth@beta

ํ„ฐ๋ฏธ๋„์— ์ž…๋ ฅํ•ด์„œ ๋‚˜์˜จ ๋ฌธ์ž๋ฅผ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ์ง€์ •

openssl rand -base64 33
AUTH_SECRET= ํ™˜๊ฒฝ๋ณ€์ˆ˜์ง€์ •

src/auth.ts

import NextAuth from 'next-auth';

export const { handlers, signIn, signOut, auth } = NextAuth({
  providers: [],
});

app/api/auth/[...nextauth]/route.ts

import { handlers } from '@/auth'; // Referring to the auth.ts we just created
export const { GET, POST } = handlers;

src/middleware.ts

  • ๐Ÿšจ ๋ฐฐํฌ๋ฅผ ์œ„ํ•ด bulid ํ•˜๋ฉด ํ•ด๋‹น ํŒŒ์ผ error

export { auth as middleware } from '@/auth';
  • ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉ๋กœ ๋ณ€๊ฒฝ ํ•ด์•ผํ•จ

import { NextRequest, NextResponse } from 'next/server';
export const config = {
  matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};
export async function middleware(request: NextRequest) {
  return NextResponse.next();
}

Last updated