NextAuth 카카오 로그인
install Next Auth
pnpm add next-authadd API route
app/api/auth/[...nextauth]/route.ts 파일 생성 후, providers 만들기
import NextAuth from 'next-auth';
import KakaoProvider from 'next-auth/providers/kakao';
const authOptions = NextAuth({
providers: [
// Configure one or more authentication providers
KakaoProvider({
clientId: process.env.KAKAO_CLIENT_ID!,
clientSecret: process.env.KAKAO_CLIENT_SECRET!,
}),
],
secret: process.env.AUTH_SECRET // 🔥 꼭 추가해줘야 함. (해당 코드가 없을 때 빌드했을 때 서버에러가 발생함)
});
export { authOptions as GET, authOptions as POST };
⛏️ provider 컴포넌트
SessionProvider는 client에서만 동작한다.
layout.tsx에서는 실행할 수 없으므로 따로 provider 컴포넌트를 작성한 후, 하위 컴포넌트를 감싼다.
REFERENCE
➡️ NextAuth.js
NextAuth Type Error
ERROR: "authOptions" is not a valid Route export field. app/api/auth/[...nextauth]/route.ts Type error: Route "app/api/auth/[...nextauth]/route.ts" does not match the required types of a Next.js Route.
👉🏻 authOptions가 모듈로 분리되지 않았을 때 발생
기존 파일
해결
authOptions를 @/utils/authOptions.ts 파일로 분리
app/api/auth/[...nextauth]/route.ts 에서는 authOptions import
REFERENCE
Last updated