Skip to main content

NextAuth

安裝NextAuth

npm i next-auth
  • 產生私鑰AUTH_SECRET
npx auth secret
  • 會在根目錄建立一個.env.local檔案
AUTH_SECRET="PWlTJ9ySSbhsXRRZMe4yR5nNyUnCh/S5k7248J7NH0I="

建立auth.ts檔

  • 在src資料夾內建立一個檔案auth.ts
import NextAuth from "next-auth";
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [],
})

建立Route Handler

  • 建立檔案src/app/auth/[...nextauth]/route.ts
import { handlers } from "@/auth" // Referring to the auth.ts we just created
export const { GET, POST } = handlers