This repository was archived by the owner on Jul 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
add Middleware, NEXTAUTH_SECRET, Deployment sections #218
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
11b9e79
add Middleware, NEXTAUTH_SECRET, Deployment sections
balazsorban44 1742386
hide v4 announcement header
balazsorban44 175e7d2
move Next.js docs under configuration
balazsorban44 976bf61
document Middleware API
balazsorban44 21cc7e6
address review comments
balazsorban44 cf05338
feat: new deployment page
ndom91 69ad28f
fix(docs): deployment page formatting
ndom91 f90cd03
fix(docs): address PR suggestions
ndom91 79971c8
fix(docs): PR review changes
ndom91 f512ac7
fix links, namespace token in middleware
balazsorban44 dfd907c
remove the `secret` option
balazsorban44 4c0b3ec
comment on NEXTAUTH_SECRET required with middleware
balazsorban44 fb7676f
fix(docs): add deployment page details
ndom91 5b28254
chore: simplify
ndom91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Deployment | ||
|
|
||
| Deploying NextAuth.js only requires a few steps. | ||
|
|
||
| # Vercel | ||
|
|
||
| 1. Expose [System Environment Variables](https://vercel.com/docs/concepts/projects/environment-variables#system-environment-variables). | ||
| 2. Create `NEXTAUTH_SECRET` environment variable. The value should be something random, eg.: `openssl rand -base64 32` or https://generate-secret.vercel.app/32 | ||
ndom91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 3. Add your provider's client ID and client secret to environment variables. *(Skip this step if not using an [OAuth Provider](/configuration/providers/oauth))* | ||
| 4. Deploy! | ||
ndom91 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Securing a preview deployment | ||
|
|
||
| Securing a preview deployment (with an OAuth provider) has some caveats, as most providers only allow a single redirect/callback URL, or you cannot set the value before publishing the site. Here are a few ways you can still use NextAuth.js to secure your Preview Deployments | ||
|
|
||
| ### Using the Credentials Provider | ||
|
|
||
| ... | ||
|
|
||
| https://github.com/nextauthjs/docs/issues/19 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Next.js | ||
balazsorban44 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Middleware | ||
|
|
||
| You can use a Next.js Middleware with NextAuth.js to protect your site. | ||
|
|
||
| Next.js 12 has introduced [Middleware](https://nextjs.org/docs/middleware). It is a way to run logic before accessing any page, even when they are static. On platforms like Vercel, the speed is guaranteed by executing Middleware at the [Edge](https://nextjs.org/docs/api-reference/edge-runtime) | ||
balazsorban44 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### API | ||
|
|
||
| TODO: | ||
|
|
||
| ### Caveats | ||
|
|
||
| - Currently only supports session verification, as as parts of the sign-in logic code need to run in a Node.js environment. In the future though, we would like to make sure that NextAuth.js can run fully at the [Edge](https://nextjs.org/docs/api-reference/edge-runtime) | ||
| - Only supports the `"jwt"` [session strategy](/options#session). We need to wait until databases at the Edge become mature enough to ensure a fast experience. (If you know of an Edge-compatible database, we would like if you proposed a new [Adapter](http://localhost:3000/tutorials/creating-a-database-adapter)) | ||
|
|
||
| ### Examples | ||
|
|
||
| #### Authentication for entire site | ||
|
|
||
| ```js title="pages/_middleware.js" | ||
| export { default } from "next-auth/middleware" | ||
| ``` | ||
|
|
||
| With this one line, when someone tries to load any of your pages, they will have to be logged-in first. Otherwise, they are redirected to your login page. | ||
|
|
||
| #### Authorization for certain pages | ||
|
|
||
| ```js title="pages/admin/_middleware.js" | ||
| import { withAuth } from "next-auth/middleware" | ||
|
|
||
| export default withAuth({ | ||
| authorized: ({ token }) => token?.role === "admin" | ||
| }) | ||
| ``` | ||
| With the above code, you just made sure that only user's with the `admin` role can access any of the pages under thge `/admin` route. (Including nested routes as well, like `/admin/settings` etc.). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.