Skip to content

Commit

Permalink
feat: simplify NextAuth instantiation (#867)
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 committed Dec 5, 2020
1 parent 5415a9c commit b86ffa5
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 25 deletions.
6 changes: 2 additions & 4 deletions README.md
Expand Up @@ -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({
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/server/index.js
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
6 changes: 2 additions & 4 deletions www/docs/getting-started/example.md
Expand Up @@ -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({
Expand All @@ -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.
Expand Down
6 changes: 2 additions & 4 deletions www/docs/schemas/adapters.md
Expand Up @@ -74,17 +74,15 @@ import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

const options = {
export default NextAuth({
providers: [
Providers.Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET
})
],
adapter: Adapters.Prisma.Adapter({ prisma }),
}

export default (req, res) => NextAuth(req, res, options)
})
```

:::tip
Expand Down
6 changes: 2 additions & 4 deletions www/docs/tutorials/ldap-auth.md
Expand Up @@ -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",
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 2 additions & 4 deletions www/docs/tutorials/typeorm-custom-models.md
Expand Up @@ -62,7 +62,7 @@ import Adapters from "next-auth/adapters"

import Models from "../../../models"

const options = {
export default NextAuth({
providers: [
// Your providers
],
Expand All @@ -77,9 +77,7 @@ const options = {
},
}
),
}

export default (req, res) => NextAuth(req, res, options)
})
```


6 changes: 2 additions & 4 deletions www/src/pages/index.js
Expand Up @@ -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({
Expand All @@ -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

1 comment on commit b86ffa5

@vercel
Copy link

@vercel vercel bot commented on b86ffa5 Dec 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.