|
| 1 | +--- |
| 2 | +id: xata |
| 3 | +title: Xata |
| 4 | +--- |
| 5 | + |
| 6 | +# Xata |
| 7 | + |
| 8 | +This adapter allows using next-auth with Xata as a database to store users, sessions, and more. The preferred way to create a Xata project and use Xata databases is using the [Xata Command Line Interface (CLI)](https://docs.xata.io/cli/getting-started). The CLI allows generating a `XataClient` that will help you work with Xata in a safe way, and that this adapter depends on. |
| 9 | + |
| 10 | +<!-- @todo add GIFs --> |
| 11 | + |
| 12 | +## Getting Started |
| 13 | + |
| 14 | +Let's first make sure we have everything installed and configured. We're going to need: |
| 15 | + |
| 16 | +- next-auth + adapter |
| 17 | +- the Xata CLI |
| 18 | +- to configure the CLI |
| 19 | + |
| 20 | +We can do this like so: |
| 21 | + |
| 22 | +```bash npm2yarn2pnpm |
| 23 | +# Install next-auth + adapter |
| 24 | +npm install next-auth @next-auth/xata-adapter |
| 25 | + |
| 26 | +# Install the Xata CLI globally if you don't already have it |
| 27 | +npm install --location=global @xata.io/cli |
| 28 | + |
| 29 | +# Login |
| 30 | +xata auth login |
| 31 | +``` |
| 32 | + |
| 33 | +Now that we're ready, let's create a new Xata project using our next-auth schema that the Xata adapter can work with. To do that, copy and paste this schema file into your project's directory: |
| 34 | + |
| 35 | +```json title="schema.json" |
| 36 | +{ |
| 37 | + "formatVersion": "", |
| 38 | + "tables": [ |
| 39 | + { |
| 40 | + "name": "nextauth_users", |
| 41 | + "columns": [ |
| 42 | + { |
| 43 | + "name": "email", |
| 44 | + "type": "email" |
| 45 | + }, |
| 46 | + { |
| 47 | + "name": "emailVerified", |
| 48 | + "type": "datetime" |
| 49 | + }, |
| 50 | + { |
| 51 | + "name": "name", |
| 52 | + "type": "string" |
| 53 | + }, |
| 54 | + { |
| 55 | + "name": "image", |
| 56 | + "type": "string" |
| 57 | + } |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "name": "nextauth_accounts", |
| 62 | + "columns": [ |
| 63 | + { |
| 64 | + "name": "user", |
| 65 | + "type": "link", |
| 66 | + "link": { |
| 67 | + "table": "nextauth_users" |
| 68 | + } |
| 69 | + }, |
| 70 | + { |
| 71 | + "name": "type", |
| 72 | + "type": "string" |
| 73 | + }, |
| 74 | + { |
| 75 | + "name": "provider", |
| 76 | + "type": "string" |
| 77 | + }, |
| 78 | + { |
| 79 | + "name": "providerAccountId", |
| 80 | + "type": "string" |
| 81 | + }, |
| 82 | + { |
| 83 | + "name": "refresh_token", |
| 84 | + "type": "string" |
| 85 | + }, |
| 86 | + { |
| 87 | + "name": "access_token", |
| 88 | + "type": "string" |
| 89 | + }, |
| 90 | + { |
| 91 | + "name": "expires_at", |
| 92 | + "type": "int" |
| 93 | + }, |
| 94 | + { |
| 95 | + "name": "token_type", |
| 96 | + "type": "string" |
| 97 | + }, |
| 98 | + { |
| 99 | + "name": "scope", |
| 100 | + "type": "string" |
| 101 | + }, |
| 102 | + { |
| 103 | + "name": "id_token", |
| 104 | + "type": "text" |
| 105 | + }, |
| 106 | + { |
| 107 | + "name": "session_state", |
| 108 | + "type": "string" |
| 109 | + } |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "name": "nextauth_verificationTokens", |
| 114 | + "columns": [ |
| 115 | + { |
| 116 | + "name": "identifier", |
| 117 | + "type": "string" |
| 118 | + }, |
| 119 | + { |
| 120 | + "name": "token", |
| 121 | + "type": "string" |
| 122 | + }, |
| 123 | + { |
| 124 | + "name": "expires", |
| 125 | + "type": "datetime" |
| 126 | + } |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "name": "nextauth_users_accounts", |
| 131 | + "columns": [ |
| 132 | + { |
| 133 | + "name": "user", |
| 134 | + "type": "link", |
| 135 | + "link": { |
| 136 | + "table": "nextauth_users" |
| 137 | + } |
| 138 | + }, |
| 139 | + { |
| 140 | + "name": "account", |
| 141 | + "type": "link", |
| 142 | + "link": { |
| 143 | + "table": "nextauth_accounts" |
| 144 | + } |
| 145 | + } |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "name": "nextauth_users_sessions", |
| 150 | + "columns": [ |
| 151 | + { |
| 152 | + "name": "user", |
| 153 | + "type": "link", |
| 154 | + "link": { |
| 155 | + "table": "nextauth_users" |
| 156 | + } |
| 157 | + }, |
| 158 | + { |
| 159 | + "name": "session", |
| 160 | + "type": "link", |
| 161 | + "link": { |
| 162 | + "table": "nextauth_sessions" |
| 163 | + } |
| 164 | + } |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "name": "nextauth_sessions", |
| 169 | + "columns": [ |
| 170 | + { |
| 171 | + "name": "sessionToken", |
| 172 | + "type": "string" |
| 173 | + }, |
| 174 | + { |
| 175 | + "name": "expires", |
| 176 | + "type": "datetime" |
| 177 | + }, |
| 178 | + { |
| 179 | + "name": "user", |
| 180 | + "type": "link", |
| 181 | + "link": { |
| 182 | + "table": "nextauth_users" |
| 183 | + } |
| 184 | + } |
| 185 | + ] |
| 186 | + } |
| 187 | + ] |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +Now, run the following command: |
| 192 | + |
| 193 | +```bash |
| 194 | +xata init --schema=./path/to/your/schema.json |
| 195 | +``` |
| 196 | + |
| 197 | +The CLI will walk you through a setup process where you choose a [workspace](https://docs.xata.io/concepts/workspaces) (kind of like a GitHub org or a Vercel team) and an appropriate database. We recommend using a fresh database for this, as we'll augment it with tables that next-auth needs. |
| 198 | + |
| 199 | +Once you're done, you can continue using next-auth in your project as expected, like creating a `./pages/api/auth/[...nextauth]` route. |
| 200 | + |
| 201 | +```typescript title="pages/api/auth/[...nextauth].ts" |
| 202 | +import NextAuth from "next-auth" |
| 203 | +import GoogleProvider from "next-auth/providers/google" |
| 204 | + |
| 205 | +const client = new XataClient() |
| 206 | + |
| 207 | +export default NextAuth({ |
| 208 | + providers: [ |
| 209 | + GoogleProvider({ |
| 210 | + clientId: process.env.GOOGLE_CLIENT_ID, |
| 211 | + clientSecret: process.env.GOOGLE_CLIENT_SECRET, |
| 212 | + }), |
| 213 | + ], |
| 214 | +}) |
| 215 | +``` |
| 216 | + |
| 217 | +Now to Xata-fy this route, let's add the Xata client and adapter: |
| 218 | + |
| 219 | +```diff |
| 220 | +import NextAuth from "next-auth" |
| 221 | +import GoogleProvider from "next-auth/providers/google" |
| 222 | ++import { XataAdapter } from "@next-auth/xata-adapter" |
| 223 | ++import { XataClient } from "../../../xata" // or wherever you've chosen to create the client |
| 224 | + |
| 225 | ++const client = new XataClient() |
| 226 | + |
| 227 | +export default NextAuth({ |
| 228 | ++ adapter: XataAdapter(client), |
| 229 | + providers: [ |
| 230 | + GoogleProvider({ |
| 231 | + clientId: process.env.GOOGLE_CLIENT_ID, |
| 232 | + clientSecret: process.env.GOOGLE_CLIENT_SECRET, |
| 233 | + }), |
| 234 | + ], |
| 235 | +}) |
| 236 | +``` |
| 237 | + |
| 238 | +This fully sets up your next-auth site to work with Xata. |
| 239 | + |
| 240 | +## Contributing |
| 241 | + |
| 242 | +This is an open-source project created by humans, and as such, might have a few issues. If you experience any of these, we recommend [opening issues](https://github.com/nextauthjs/next-auth/issues/new?assignees=&labels=triage&template=1_bug_framework.yml&title=Issue%20on%20Xata%20adapter&description=I%20experienced%20this%20issue:\n##%20Reproduction%20Steps:\n\n-) that can help us solve problems and build reliable software. |
0 commit comments