From b86ffa5dd55263e462ba00c8fe9801eea78d5e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Sat, 5 Dec 2020 10:34:32 +0100 Subject: [PATCH] feat: simplify NextAuth instantiation (#867) --- README.md | 6 ++---- src/server/index.js | 10 +++++++++- www/docs/getting-started/example.md | 6 ++---- www/docs/schemas/adapters.md | 6 ++---- www/docs/tutorials/ldap-auth.md | 6 ++---- www/docs/tutorials/typeorm-custom-models.md | 6 ++---- www/src/pages/index.js | 6 ++---- 7 files changed, 21 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 5cad3a447e..8f883f3348 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ Alternatively you can raise a PR directly with your fixes on [**DefinitelyTyped* import NextAuth from 'next-auth' import Providers from 'next-auth/providers' -const options = { +export default NextAuth({ providers: [ // OAuth authentication providers Providers.Apple({ @@ -87,9 +87,7 @@ const options = { ], // SQL or MongoDB database (or leave empty) database: process.env.DATABASE_URL -} - -export default (req, res) => NextAuth(req, res, options) +}) ``` ### Add React Component diff --git a/src/server/index.js b/src/server/index.js index 8c8cbb29b5..2b47a2e635 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -21,7 +21,7 @@ if (!process.env.NEXTAUTH_URL) { logger.warn('NEXTAUTH_URL', 'NEXTAUTH_URL environment variable not set') } -export default async (req, res, userSuppliedOptions) => { +async function NextAuth (req, res, userSuppliedOptions) { // To the best of my knowledge, we need to return a promise here // to avoid early termination of calls to the serverless function // (and then return that promise when we are done) - eslint @@ -313,3 +313,11 @@ export default async (req, res, userSuppliedOptions) => { } }) } + +export default async (...args) => { + if (args.length === 1) { + return (req, res) => NextAuth(req, res, args[0]) + } + + return NextAuth(...args) +} diff --git a/www/docs/getting-started/example.md b/www/docs/getting-started/example.md index e223800856..8b7eb32516 100644 --- a/www/docs/getting-started/example.md +++ b/www/docs/getting-started/example.md @@ -21,7 +21,7 @@ To add NextAuth.js to a project create a file called `[...nextauth].js` in `page import NextAuth from 'next-auth' import Providers from 'next-auth/providers' -const options = { +export default NextAuth({ // Configure one or more authentication providers providers: [ Providers.GitHub({ @@ -33,9 +33,7 @@ const options = { // A database is optional, but required to persist accounts in a database database: process.env.DATABASE_URL, -} - -export default (req, res) => NextAuth(req, res, options) +}) ``` All requests to `/api/auth/*` (signin, callback, signout, etc) will automatically be handed by NextAuth.js. diff --git a/www/docs/schemas/adapters.md b/www/docs/schemas/adapters.md index 8547de14db..f08bb14e6b 100644 --- a/www/docs/schemas/adapters.md +++ b/www/docs/schemas/adapters.md @@ -74,7 +74,7 @@ import { PrismaClient } from '@prisma/client' const prisma = new PrismaClient() -const options = { +export default NextAuth({ providers: [ Providers.Google({ clientId: process.env.GOOGLE_CLIENT_ID, @@ -82,9 +82,7 @@ const options = { }) ], adapter: Adapters.Prisma.Adapter({ prisma }), -} - -export default (req, res) => NextAuth(req, res, options) +}) ``` :::tip diff --git a/www/docs/tutorials/ldap-auth.md b/www/docs/tutorials/ldap-auth.md index a5c19b9a49..12a273e772 100644 --- a/www/docs/tutorials/ldap-auth.md +++ b/www/docs/tutorials/ldap-auth.md @@ -14,7 +14,7 @@ const ldap = require("ldapjs"); import NextAuth from "next-auth"; import Providers from "next-auth/providers"; -const options = { +export default NextAuth({ providers: [ Providers.Credentials({ name: "LDAP", @@ -64,9 +64,7 @@ const options = { secret: process.env.NEXTAUTH_SECRET, encryption: true, // Very important to encrypt the JWT, otherwise you're leaking username+password into the browser }, -}; - -export default (req, res) => NextAuth(req, res, options); +}); ``` 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. diff --git a/www/docs/tutorials/typeorm-custom-models.md b/www/docs/tutorials/typeorm-custom-models.md index dcfa8e46c5..1fc9f1e218 100644 --- a/www/docs/tutorials/typeorm-custom-models.md +++ b/www/docs/tutorials/typeorm-custom-models.md @@ -62,7 +62,7 @@ import Adapters from "next-auth/adapters" import Models from "../../../models" -const options = { +export default NextAuth({ providers: [ // Your providers ], @@ -77,9 +77,7 @@ const options = { }, } ), -} - -export default (req, res) => NextAuth(req, res, options) +}) ``` diff --git a/www/src/pages/index.js b/www/src/pages/index.js index 565df70f2f..5ef1f9f64d 100644 --- a/www/src/pages/index.js +++ b/www/src/pages/index.js @@ -219,7 +219,7 @@ const serverlessFunctionCode = ` import NextAuth from 'next-auth' import Providers from 'next-auth/providers' -const options = { +export default NextAuth({ providers: [ // OAuth authentication providers... Providers.Apple({ @@ -242,9 +242,7 @@ const options = { ], // Optional SQL or MongoDB database to persist users database: process.env.DATABASE_URL -} - -export default (req, res) => NextAuth(req, res, options) +}) `.trim() export default Home