> yarn add next-auth
> NEXTAUTH_URL=http://localhost:3000
> NEXTAUTH_SECRET은 랜덤한 string 값으로 설정 : openssl rand -base64 24
import NextAuth from "next-auth"
import GoogleProvider from "next-auth/providers/google"
export const authOptions = {
// Configure one or more authentication providers
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
// ...add more providers here
],
}
export default NextAuth(authOptions)
// pages/_app.tsx
import { SessionProvider } from "next-auth/react"
export default function App({
Component,
pageProps: { session, ...pageProps },
}) {
return (
<SessionProvider session={session}>
<Component {...pageProps} />
</SessionProvider>
)
}
export default function LoginPage() {
const { status, data: session } = useSession();
const router = useRouter();
useEffect(() => {
if (status === "authenticated") {
router.replace("/");
}
}, [router, status]);
return (
<button
type="button"
className="text-white flex gap-2 bg-[#4285f4] hover:bg-[#4285f4]/90 font-medium rounded-lg w-full px-5 py-4 text-center items-center justify-center"
onClick={() => signIn("google", { callbackUrl: "/" })}
>
<AiOutlineGoogle className="w-6 h-6" />
Sign in With Google
</button>
> yarn add @auth/prisma-adapter
➰ Prisma Adapter란 ?
Next-auth로 로그인 또는 회원가입한 사용자들의 계정 정보와 세션/토큰 정보를 저장할 수 있도록 연동하는 것 (자동으로 prisma에 맞게 데이터 생성 가능)
import NextAuth from "next-auth"
import Providers from "next-auth/providers"
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import { PrismaClient } from "@prisma/client"
const prisma = new PrismaClient()
export default NextAuth({
providers: [
Providers.Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
],
adapter: PrismaAdapter(prisma),
})
model Account {
id String @id @default(cuid())
userId Int
type String
provider String
providerAccountId String
refresh_token String? @db.Text
refresh_token_expires_in Int?
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
likes Like[]
comments Comment[]
}
model Session {
id String @id @default(cuid())
sessionToken String @unique
userId Int
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
model VerificationToken {
identifier String
token String @unique
expires DateTime
@@unique([identifier, token])
}
> npx prisma migrate dev
https://next-auth.js.org/configuration/nextjs#middleware
export { default } from "next-auth/middleware"
export const config = { matcher: ["/users/mypage", "/stores/new", "/stores/:id/edit", "/users/likes"] }
💡
공식문서 참고