跳至内容
从 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