Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simplify NextAuth instantiation #867

Merged
merged 4 commits into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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