feat(adapter): remove adapters from core (#1919)

* feat(adapter): remove built-in adapters and database

BREAKING CHANGE:

From now on, you will have to import your own adapter

Check out https://github.com/nextauthjs/adapters

The migration is super easy and has HUGE advantages for those not using TypeORM.

```diff
// [...nextauth].js
+ import TypeORMAdapter from "@next-auth/typeorm-legacy-adapter"
import NextAuth from "next-auth"

...
export default NextAuth({
-  database: "yourconnectionstring",
+ adapter: TypeORMAdapter("yourconnectionstring")
})
```


Co-authored-by: Lluis Agusti <hi@llu.lu>
Co-authored-by: Giovanni Carnel <479046+g10@users.noreply.github.com>
This commit is contained in:
Balázs Orbán
2021-06-09 14:45:13 +02:00
committed by GitHub
parent d29e3e9c9d
commit 960bc1e9c0
21 changed files with 4752 additions and 10469 deletions

View File

@@ -17,7 +17,9 @@ jobs:
- name: Init
uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v1
uses: actions/setup-node@v2
with:
node-version: "16"
- name: Dependencies
uses: bahmutov/npm-install@v1
- name: Run tests
@@ -32,7 +34,9 @@ jobs:
- name: Init
uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v1
uses: actions/setup-node@v2
with:
node-version: "16"
- name: Dependencies
uses: bahmutov/npm-install@v1
- name: Release

View File

@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx pretty-quick --staged
# npx pretty-quick --staged

View File

@@ -110,8 +110,6 @@ export default NextAuth({
from: "<no-reply@example.com>",
}),
],
// SQL or MongoDB database (or leave empty)
database: process.env.DATABASE_URL,
})
```

View File

@@ -5,10 +5,6 @@ import Auth0Provider from "next-auth/providers/auth0"
import TwitterProvider from "next-auth/providers/twitter"
import CredentialsProvider from "next-auth/providers/credentials"
// import Adapters from 'next-auth/adapters'
// import { PrismaClient } from '@prisma/client'
// const prisma = new PrismaClient()
export default NextAuth({
// Used to debug https://github.com/nextauthjs/next-auth/issues/1664
// cookies: {
@@ -79,13 +75,4 @@ export default NextAuth({
},
debug: false,
theme: "auto",
// Default Database Adapter (TypeORM)
// database: process.env.DATABASE_URL
// Prisma Database Adapter
// To configure this app to use the schema in `prisma/schema.prisma` run:
// npx prisma generate
// npx prisma migrate dev
// adapter: Adapters.Prisma.Adapter({ prisma })
})

View File

@@ -15,7 +15,6 @@ const MODULE_ENTRIES = {
const BUILD_TARGETS = {
[`${MODULE_ENTRIES.SERVER}.js`]: "module.exports = require('./dist/server').default\n",
[`${MODULE_ENTRIES.CLIENT}.js`]: "module.exports = require('./dist/client').default\n",
[`${MODULE_ENTRIES.ADAPTERS}.js`]: "module.exports = require('./dist/adapters').default\n",
[`${MODULE_ENTRIES.PROVIDERS}.js`]: "module.exports = require('./dist/providers').default\n",
[`${MODULE_ENTRIES.JWT}.js`]: "module.exports = require('./dist/lib/jwt').default\n",
[`${MODULE_ENTRIES.ERRORS}.js`]: "module.exports = require('./dist/lib/errors').default\n",

14946
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -22,7 +22,6 @@
"exports": {
".": "./dist/server/index.js",
"./jwt": "./dist/lib/jwt.js",
"./adapters": "./dist/adapters/index.js",
"./client": "./dist/client/index.js",
"./providers": "./dist/providers/index.js",
"./providers/*": "./dist/providers/*.js",
@@ -73,6 +72,7 @@
"preact-render-to-string": "^5.1.14"
},
"peerDependencies": {
"nodemailer": "^6.4.16",
"react": "^16.13.1 || ^17",
"react-dom": "^16.13.1 || ^17"
},

View File

@@ -1,10 +0,0 @@
import * as TypeORM from "./typeorm"
import * as Prisma from "./prisma"
export { TypeORM, Prisma }
export default {
Default: TypeORM.Adapter,
TypeORM,
Prisma,
}

View File

@@ -1,6 +0,0 @@
/*
* Source code can be found at:
* https://github.com/nextauthjs/adapters/tree/canary/packages/prisma-legacy
*/
export { PrismaLegacyAdapter as Adapter } from "@next-auth/prisma-legacy-adapter"

View File

@@ -1,9 +0,0 @@
/*
* Source code can be found at:
* https://github.com/nextauthjs/adapters/tree/canary/packages/typeorm-legacy
*/
export {
TypeORMLegacyAdapter as Adapter,
Models,
} from "@next-auth/typeorm-legacy-adapter"

View File

@@ -1,5 +1,5 @@
import logger from '../lib/logger'
import requireOptional from '@balazsorban/require-optional'
import nodemailer from "nodemailer"
export default function Email(options) {
return {
@@ -27,7 +27,6 @@ async function sendVerificationRequest ({ identifier: email, url, baseUrl, provi
// Strip protocol from URL and use domain as site name
const site = baseUrl.replace(/^https?:\/\//, '')
try {
const nodemailer = requireOptional('nodemailer')
await nodemailer
.createTransport(server)
.sendMail({

View File

@@ -14,8 +14,6 @@ import csrfTokenHandler from "./lib/csrf-token-handler"
import * as pkce from "./lib/oauth/pkce-handler"
import * as state from "./lib/oauth/state-handler"
import optionalRequire from "@balazsorban/require-optional"
// To work properly in production with OAuth providers the NEXTAUTH_URL
// environment variable must be set.
if (!process.env.NEXTAUTH_URL) {
@@ -113,7 +111,6 @@ async function NextAuthHandler(req, res, userOptions) {
...userOptions,
// These computed settings can have values in userOptions but we override them
// and are request-specific.
adapter,
baseUrl,
basePath,
action,

32
types/adapters.d.ts vendored
View File

@@ -2,38 +2,6 @@ import { AppOptions } from "./internals"
import { User, Profile, Session } from "."
import { EmailConfig } from "./providers"
/** Legacy */
export {
TypeORMAccountModel,
TypeORMSessionModel,
TypeORMUserModel,
TypeORMVerificationRequestModel,
} from "@next-auth/typeorm-legacy-adapter"
import {
TypeORMAdapter,
TypeORMAdapterModels,
} from "@next-auth/typeorm-legacy-adapter"
import { PrismaLegacyAdapter } from "@next-auth/prisma-legacy-adapter"
export const TypeORM: {
Models: TypeORMAdapterModels
Adapter: TypeORMAdapter
}
export const Prisma: {
Adapter: PrismaLegacyAdapter
}
declare const Adapters: {
Default: TypeORMAdapter
TypeORM: typeof TypeORM
Prisma: typeof Prisma
}
export default Adapters
/**
* Using a custom adapter you can connect to any database backend or even several different databases.
* Custom adapters created and maintained by our community can be found in the adapters repository.

19
types/index.d.ts vendored
View File

@@ -29,14 +29,6 @@ export interface NextAuthOptions {
* [Documentation](https://next-auth.js.org/configuration/options#providers) | [Providers documentation](https://next-auth.js.org/configuration/providers)
*/
providers: AppProviders
/**
* A database connection string or configuration object.
* * **Default value**: `null`
* * **Required**: *No (unless using email provider)*
*
* [Documentation](https://next-auth.js.org/configuration/options#database) | [Databases](https://next-auth.js.org/configuration/databases)
*/
database?: string | Record<string, any> | ConnectionOptions
/**
* A random string used to hash tokens, sign cookies and generate cryptographic keys.
* If not specified is uses a hash of all configuration options, including Client ID / Secrets for entropy.
@@ -113,18 +105,11 @@ export interface NextAuthOptions {
*/
events?: Partial<JWTEventCallbacks | SessionEventCallbacks>
/**
* By default NextAuth.js uses a database adapter that uses TypeORM and supports MySQL, MariaDB, Postgres and MongoDB and SQLite databases.
* An alternative adapter that uses Prisma, which currently supports MySQL, MariaDB and Postgres, is also included.
* You can use the adapter option to use the Prisma adapter - or pass in your own adapter
* if you want to use a database that is not supported by one of the built-in adapters.
* * **Default value**: TypeORM adapter
* You can use the adapter option to pass in your database adapter.
*
* * **Required**: *No*
*
* - ⚠ If the `adapter` option is specified it overrides the `database` option, only specify one or the other.
* - ⚠ Adapters are being migrated to their own home in a Community maintained repository.
*
* [Documentation](https://next-auth.js.org/configuration/options#adapter) |
* [Default adapter](https://next-auth.js.org/schemas/adapters#typeorm-adapter) |
* [Community adapters](https://github.com/nextauthjs/adapters)
*/
adapter?: ReturnType<Adapter>

View File

@@ -1,26 +1 @@
import Adapters from "next-auth/adapters"
// ExpectType TypeORMAdapter["Adapter"]
Adapters.Default({
type: "sqlite",
database: ":memory:",
synchronize: true,
})
// ExpectType TypeORMAdapter
Adapters.TypeORM.Adapter({
type: "sqlite",
database: ":memory:",
synchronize: true,
})
// ExpectType PrismaAdapter
Adapters.Prisma.Adapter({
prisma: {},
modelMapping: {
User: "foo",
Account: "bar",
Session: "session",
VerificationRequest: "foo",
},
})
// TODO:

View File

@@ -135,7 +135,6 @@ const allConfig: NextAuthTypes.NextAuthOptions = {
clientSecret: "123",
}),
],
database: "path/to/db",
debug: true,
secret: "my secret",
session: {

View File

@@ -23,7 +23,7 @@ Configure your NextAuth.js to use the Prisma Adapter:
```javascript title="pages/api/auth/[...nextauth].js"
import NextAuth from "next-auth"
import Providers from "next-auth/providers"
import Adapters from "next-auth/adapters"
import { PrismaLegacyAdapter } from "@next-auth/prisma-legacy-adapter"
import { PrismaClient } from "@prisma/client"
const prisma = new PrismaClient()
@@ -35,7 +35,7 @@ export default NextAuth({
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
],
adapter: Adapters.Prisma.Adapter({ prisma }),
adapter: PrismaLegacyAdapter({ prisma }),
})
```
@@ -161,7 +161,7 @@ You can use custom model names by using the `modelMapping` option (shown here wi
```javascript title="pages/api/auth/[...nextauth].js"
...
adapter: Adapters.Prisma.Adapter({
adapter: PrismaLegacyAdapter({
prisma,
modelMapping: {
User: 'user',

View File

@@ -26,24 +26,31 @@ To learn more about databases in NextAuth.js and how they are used, check out [d
## How to use a database
You can specify database credentials as as a connection string or a [TypeORM configuration](https://github.com/typeorm/typeorm/blob/master/docs/using-ormconfig.md) object.
## How to use a database
The following approaches are exactly equivalent:
You can specify database credentials as a [TypeORM configuration](https://github.com/typeorm/typeorm/blob/master/docs/using-ormconfig.md) object or connection string:
```js
database: "mysql://nextauth:password@127.0.0.1:3306/database_name"
```js title="pages/api/auth/[...nextauth].js"
import TypeORMAdapter from "@next-auth/typeorm-legacy-adapter"
import NextAuth from "next-auth"
export default NextAuth({
adapter: TypeORMAdapter(
"mysql://nextauth:password@127.0.0.1:3306/database_name"
),
// or...
adapter: TypeORMAdapter({
type: "mysql",
host: "127.0.0.1",
port: 3306,
username: "nextauth",
password: "password",
database: "database_name",
}),
})
```
```js
database: {
type: 'mysql',
host: '127.0.0.1',
port: 3306,
username: 'nextauth',
password: 'password',
database: 'database_name'
}
```
Both approaches are exactly equivalent:
:::tip
You can pass in any valid [TypeORM configuration option](https://github.com/typeorm/typeorm/blob/master/docs/using-ormconfig.md).
@@ -51,21 +58,23 @@ You can pass in any valid [TypeORM configuration option](https://github.com/type
_e.g. To set a prefix for all table names you can use the **entityPrefix** option as connection string parameter:_
```js
"mysql://nextauth:password@127.0.0.1:3306/database_name?entityPrefix=nextauth_"
adapter: TypeORMAdapter(
"mysql://nextauth:password@127.0.0.1:3306/database_name?entityPrefix=nextauth_"
)
```
_…or as a database configuration object:_
```js
database: {
type: 'mysql',
host: '127.0.0.1',
adapter: TypeORMAdapter({
type: "mysql",
host: "127.0.0.1",
port: 3306,
username: 'nextauth',
password: 'password',
database: 'database_name',
entityPrefix: 'nextauth_'
}
username: "nextauth",
password: "password",
database: "database_name",
entityPrefix: "nextauth_",
})
```
:::
@@ -86,19 +95,21 @@ _If you are running SQLite, MongoDB or a Document database you can skip this ste
Alternatively, you can also have your database configured automatically using the `synchronize: true` option:
```js
database: "mysql://nextauth:password@127.0.0.1:3306/database_name?synchronize=true"
adapter: TypeORMAdapter(
"mysql://nextauth:password@127.0.0.1:3306/database_name?synchronize=true"
)
```
```js
database: {
type: 'mysql',
host: '127.0.0.1',
adapter: TypeORMAdapter({
type: "mysql",
host: "127.0.0.1",
port: 3306,
username: 'nextauth',
password: 'password',
database: 'database_name',
synchronize: true
}
username: "nextauth",
password: "password",
database: "database_name",
synchronize: true,
})
```
:::warning
@@ -127,7 +138,9 @@ Install module:
#### Example
```js
database: "mysql://username:password@127.0.0.1:3306/database_name"
adapter: TypeORMAdapter(
"mysql://username:password@127.0.0.1:3306/database_name"
)
```
### MariaDB
@@ -138,7 +151,9 @@ Install module:
#### Example
```js
database: "mariadb://username:password@127.0.0.1:3306/database_name"
adapter: TypeORMAdapter(
"mariadb://username:password@127.0.0.1:3306/database_name"
)
```
### Postgres / CockroachDB
@@ -151,30 +166,34 @@ Install module:
PostgresDB
```js
database: "postgres://username:password@127.0.0.1:5432/database_name"
adapter: TypeORMAdapter(
"postgres://username:password@127.0.0.1:5432/database_name"
)
```
CockroachDB
```js
database: "postgres://username:password@127.0.0.1:26257/database_name"
adapter: TypeORMAdapter(
"postgres://username:password@127.0.0.1:26257/database_name"
)
```
If the node is using Self-signed cert
```js
database: {
type: "cockroachdb",
host: process.env.DATABASE_HOST,
port: 26257,
username: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
ssl: {
rejectUnauthorized: false,
ca: fs.readFileSync('/path/to/server-certificates/root.crt').toString()
},
adapter: TypeORMAdapter({
type: "cockroachdb",
host: process.env.DATABASE_HOST,
port: 26257,
username: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
ssl: {
rejectUnauthorized: false,
ca: fs.readFileSync("/path/to/server-certificates/root.crt").toString(),
},
})
```
Read more: [https://node-postgres.com/features/ssl](https://node-postgres.com/features/ssl)
@@ -189,7 +208,7 @@ Install module:
#### Example
```js
database: "mssql://sa:password@localhost:1433/database_name"
adapter: TypeORMAdapter("mssql://sa:password@localhost:1433/database_name")
```
### MongoDB
@@ -200,7 +219,9 @@ Install module:
#### Example
```js
database: "mongodb://username:password@127.0.0.1:3306/database_name"
adapter: TypeORMAdapter(
"mongodb://username:password@127.0.0.1:3306/database_name"
)
```
### SQLite
@@ -213,7 +234,7 @@ Install module:
#### Example
```js
database: "sqlite://localhost/:memory:"
adapter: TypeORMAdapter("sqlite://localhost/:memory:")
```
## Other databases

View File

@@ -30,9 +30,6 @@ export default NextAuth({
}),
// ...add more providers here
],
// A database is optional, but required to persist accounts in a database
database: process.env.DATABASE_URL,
})
```

View File

@@ -136,7 +136,6 @@ You can also [use a custom sign in page](/configuration/pages#credentials-sign-i
<Image src="/img/signin-complex.png"/>
export const Image = ({ children, src, alt = '' }) => (
<div
style={{
padding: '0.2rem',
@@ -146,4 +145,4 @@ export const Image = ({ children, src, alt = '' }) => (
}}>
<img alt={alt} src={src} />
</div>
)
)

View File

@@ -267,9 +267,7 @@ export default NextAuth({
server: process.env.MAIL_SERVER,
from: 'NextAuth.js <no-reply@example.com>'
}),
],
// Optional SQL or MongoDB database to persist users
database: process.env.DATABASE_URL
]
})
`.trim()