跳至內容
從 NextAuth.js v4 遷移?請閱讀 我們的遷移指南.

Xata 轉接器

資源

設定

安裝

npm install @auth/xata-adapter
# Install the Xata CLI globally if you don't already have it
npm install --location=global @xata.io/cli
 
# Login
xata auth login

組態

./auth.ts
import NextAuth from "next-auth"
import { XataAdapter } from "@auth/xata-adapter"
import { XataClient } from "../../../xata" // Or wherever you've chosen for the generated client
 
const client = new XataClient()
 
export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: XataAdapter(client),
  providers: [],
})

Xata 設定

此轉接器允許將 Auth.js 與 Xata 作為資料庫一起使用,以儲存使用者、會話等。建立 Xata 專案和使用 Xata 資料庫的首選方式是使用Xata 命令列介面 (CLI)

CLI 允許產生一個 XataClient,可協助您以安全的方式使用 Xata,而此轉接器依賴於此。

當您準備好時,讓我們使用我們的 Auth.js 結構描述建立一個新的 Xata 專案,Xata 轉接器可以使用該結構描述。為此,請將此結構描述檔案複製並貼到專案的目錄中

schema.json
{
  "tables": [
    {
      "name": "nextauth_users",
      "columns": [
        {
          "name": "email",
          "type": "email"
        },
        {
          "name": "emailVerified",
          "type": "datetime"
        },
        {
          "name": "name",
          "type": "string"
        },
        {
          "name": "image",
          "type": "string"
        }
      ]
    },
    {
      "name": "nextauth_accounts",
      "columns": [
        {
          "name": "user",
          "type": "link",
          "link": {
            "table": "nextauth_users"
          }
        },
        {
          "name": "type",
          "type": "string"
        },
        {
          "name": "provider",
          "type": "string"
        },
        {
          "name": "providerAccountId",
          "type": "string"
        },
        {
          "name": "refresh_token",
          "type": "string"
        },
        {
          "name": "access_token",
          "type": "string"
        },
        {
          "name": "expires_at",
          "type": "int"
        },
        {
          "name": "token_type",
          "type": "string"
        },
        {
          "name": "scope",
          "type": "string"
        },
        {
          "name": "id_token",
          "type": "text"
        },
        {
          "name": "session_state",
          "type": "string"
        }
      ]
    },
    {
      "name": "nextauth_verificationTokens",
      "columns": [
        {
          "name": "identifier",
          "type": "string"
        },
        {
          "name": "token",
          "type": "string"
        },
        {
          "name": "expires",
          "type": "datetime"
        }
      ]
    },
    {
      "name": "nextauth_users_accounts",
      "columns": [
        {
          "name": "user",
          "type": "link",
          "link": {
            "table": "nextauth_users"
          }
        },
        {
          "name": "account",
          "type": "link",
          "link": {
            "table": "nextauth_accounts"
          }
        }
      ]
    },
    {
      "name": "nextauth_users_sessions",
      "columns": [
        {
          "name": "user",
          "type": "link",
          "link": {
            "table": "nextauth_users"
          }
        },
        {
          "name": "session",
          "type": "link",
          "link": {
            "table": "nextauth_sessions"
          }
        }
      ]
    },
    {
      "name": "nextauth_sessions",
      "columns": [
        {
          "name": "sessionToken",
          "type": "string"
        },
        {
          "name": "expires",
          "type": "datetime"
        },
        {
          "name": "user",
          "type": "link",
          "link": {
            "table": "nextauth_users"
          }
        }
      ]
    }
  ]
}

現在,執行以下命令

xata init --schema=./path/to/your/schema.json

CLI 將引導您完成設定過程,您可以在其中選擇一個工作區(類似於 GitHub 組織或 Vercel 團隊)和一個合適的資料庫。我們建議為此使用新的資料庫,因為我們將使用 Auth.js 需要的資料表來擴充它。

Auth.js © Balázs Orbán 和團隊 -2024