Skip to content

Commit 51cfec9

Browse files
committed
feat: simplify NextAuth instantiation (#911)
1 parent 78fd783 commit 51cfec9

File tree

7 files changed

+21
-25
lines changed

7 files changed

+21
-25
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Alternatively you can raise a PR directly with your fixes on [**DefinitelyTyped*
6868
import NextAuth from 'next-auth'
6969
import Providers from 'next-auth/providers'
7070

71-
const options = {
71+
export default NextAuth({
7272
providers: [
7373
// OAuth authentication providers
7474
Providers.Apple({
@@ -87,9 +87,7 @@ const options = {
8787
],
8888
// SQL or MongoDB database (or leave empty)
8989
database: process.env.DATABASE_URL
90-
}
91-
92-
export default (req, res) => NextAuth(req, res, options)
90+
})
9391
```
9492

9593
### Add React Component

src/server/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if (!process.env.NEXTAUTH_URL) {
2121
logger.warn('NEXTAUTH_URL', 'NEXTAUTH_URL environment variable not set')
2222
}
2323

24-
export default async (req, res, userSuppliedOptions) => {
24+
async function NextAuth (req, res, userSuppliedOptions) {
2525
// To the best of my knowledge, we need to return a promise here
2626
// to avoid early termination of calls to the serverless function
2727
// (and then return that promise when we are done) - eslint
@@ -313,3 +313,11 @@ export default async (req, res, userSuppliedOptions) => {
313313
}
314314
})
315315
}
316+
317+
export default async (...args) => {
318+
if (args.length === 1) {
319+
return (req, res) => NextAuth(req, res, args[0])
320+
}
321+
322+
return NextAuth(...args)
323+
}

www/docs/getting-started/example.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ To add NextAuth.js to a project create a file called `[...nextauth].js` in `page
2121
import NextAuth from 'next-auth'
2222
import Providers from 'next-auth/providers'
2323

24-
const options = {
24+
export default NextAuth({
2525
// Configure one or more authentication providers
2626
providers: [
2727
Providers.GitHub({
@@ -33,9 +33,7 @@ const options = {
3333

3434
// A database is optional, but required to persist accounts in a database
3535
database: process.env.DATABASE_URL,
36-
}
37-
38-
export default (req, res) => NextAuth(req, res, options)
36+
})
3937
```
4038

4139
All requests to `/api/auth/*` (signin, callback, signout, etc) will automatically be handed by NextAuth.js.

www/docs/schemas/adapters.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,15 @@ import { PrismaClient } from '@prisma/client'
7474

7575
const prisma = new PrismaClient()
7676

77-
const options = {
77+
export default NextAuth({
7878
providers: [
7979
Providers.Google({
8080
clientId: process.env.GOOGLE_CLIENT_ID,
8181
clientSecret: process.env.GOOGLE_CLIENT_SECRET
8282
})
8383
],
8484
adapter: Adapters.Prisma.Adapter({ prisma }),
85-
}
86-
87-
export default (req, res) => NextAuth(req, res, options)
85+
})
8886
```
8987

9088
:::tip

www/docs/tutorials/ldap-auth.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const ldap = require("ldapjs");
1414
import NextAuth from "next-auth";
1515
import Providers from "next-auth/providers";
1616

17-
const options = {
17+
export default NextAuth({
1818
providers: [
1919
Providers.Credentials({
2020
name: "LDAP",
@@ -64,9 +64,7 @@ const options = {
6464
secret: process.env.NEXTAUTH_SECRET,
6565
encryption: true, // Very important to encrypt the JWT, otherwise you're leaking username+password into the browser
6666
},
67-
};
68-
69-
export default (req, res) => NextAuth(req, res, options);
67+
});
7068
```
7169

7270
The idea is that once one is authenticated with the LDAP server, one can pass through both the username/DN and password to the JWT stored in the browser.

www/docs/tutorials/typeorm-custom-models.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import Adapters from "next-auth/adapters"
6262

6363
import Models from "../../../models"
6464

65-
const options = {
65+
export default NextAuth({
6666
providers: [
6767
// Your providers
6868
],
@@ -77,9 +77,7 @@ const options = {
7777
},
7878
}
7979
),
80-
}
81-
82-
export default (req, res) => NextAuth(req, res, options)
80+
})
8381
```
8482

8583

www/src/pages/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ const serverlessFunctionCode = `
219219
import NextAuth from 'next-auth'
220220
import Providers from 'next-auth/providers'
221221
222-
const options = {
222+
export default NextAuth({
223223
providers: [
224224
// OAuth authentication providers...
225225
Providers.Apple({
@@ -242,9 +242,7 @@ const options = {
242242
],
243243
// Optional SQL or MongoDB database to persist users
244244
database: process.env.DATABASE_URL
245-
}
246-
247-
export default (req, res) => NextAuth(req, res, options)
245+
})
248246
`.trim()
249247

250248
export default Home

0 commit comments

Comments
 (0)