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 のセットアップ
このアダプターを使用すると、ユーザー、セッションなどを保存するデータベースとして Xata を使用して Auth.js を使用できます。 Xata プロジェクトを作成し、Xata データベースを使用する推奨方法は、Xata コマンドラインインターフェイス (CLI) を使用することです。
CLI を使用すると、Xata を安全に操作するのに役立つ XataClient
を生成でき、このアダプターはこれに依存しています。
準備ができたら、Xata アダプターが使用できる Auth.js スキーマを使用して、新しい 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 が必要とするテーブルを追加するため、これには新しいデータベースを使用することをお勧めします。