-
Notifications
You must be signed in to change notification settings - Fork 89
feat: ✨ add out-of-the-box support for NextAuth.js #1309
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
1109a7d
feat: :sparkles: add out-of-the-box support for NextAuth.js
ericapisani 853a7b3
test: :white_check_mark: add test coverage for the setting of NEXTAUT…
ericapisani b4713a3
style: disable lint rules for line determining if package is installed
ericapisani 84cebe9
style: lint
ericapisani 01f6b3e
chore: remove unnecessary line in test
ericapisani fde4fe0
test: need to invoke the 'toBeUndefined' method
ericapisani ef2830b
Merge branch 'main' into ep/next-auth-support
ericapisani 10b745e
Merge branch 'main' into ep/next-auth-support
ericapisani 781c0e9
Merge branch 'main' into ep/next-auth-support
ericapisani eceb794
refactor: move logic for setting NEXTAUTH_URL to onBuild step rather …
ericapisani b3bd617
style: lint
ericapisani 741c189
test: add NextAuth.js demo project for use in testing plugin function…
ericapisani 9cddbaf
chore: add lint file, fix lint issues, and add project to workspace
ericapisani 29c367b
chore: forgot to save the README
ericapisani 79ba32f
style: disable lint rule for existence check of package
ericapisani 7a645f9
style: update lint rules that are being disabled
ericapisani c5a4999
style: some README changes
ericapisani 0833505
refactor: :recycle: address code review comments
ericapisani 5d16dc9
Merge branch 'main' into ep/next-auth-support
ericapisani d58129d
chore: rename demo site to follow conventions in project
ericapisani 99dfc1d
style: lint
ericapisani 9bf340c
style: small changes to package.json
ericapisani e745087
chore: forgot to update the project name in the workspaces entry
ericapisani 397ae1b
style: lint
ericapisani e567dd2
style: lint
ericapisani 30969a0
style: lint
ericapisani de39af2
style: lint
ericapisani 98cf62d
chore(deps): add husky and npm-run-all as deps
ericapisani caeb9d3
chore: add jest deps
ericapisani 2b274f3
chore: more deps
ericapisani 63850e7
chore: remove demo from workspaces
ericapisani 592621a
Merge branch 'main' into ep/next-auth-support
ericapisani 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
NEXTAUTH_URL=http://localhost:3000 | ||
|
||
NETLIFY_CLIENT_ID= | ||
NETLIFY_CLIENT_SECRET= |
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,4 @@ | ||
{ | ||
"extends": "next", | ||
"root": true | ||
} |
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 @@ | ||
.env.local | ||
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,15 @@ | ||
ISC License | ||
|
||
Copyright (c) 2018-2021, Iain Collins | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
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,32 @@ | ||
## NextAuth.js Example | ||
|
||
This example project came from the [next-auth-example](https://github.com/nextauthjs/next-auth-example) project provided by the NextAuth.js and was modified to use Netlify as the authentication provider as an example. | ||
|
||
For more details on how to get set up and configured with various providers, visit the link above. | ||
|
||
### Running locally | ||
|
||
``` | ||
npm install | ||
npm run dev | ||
``` | ||
|
||
### Configuring your local environment | ||
|
||
Copy the .env.local.example file in this directory to .env.local (which will be ignored by Git): | ||
|
||
``` | ||
cp .env.local.example .env.local | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [dust] We could improve the DX by running |
||
``` | ||
|
||
Add details for the [Netlify Provider](https://next-auth.js.org/providers/netlify). | ||
|
||
To add a Netlify OAuth application: | ||
* Visit `https://app.netlify.com/user/applications` | ||
* Click 'New OAuth App' | ||
* Enter an application name | ||
* Enter a redirect URI (for the purposes of this demo application you would use `http://localhost:3000/api/auth/callback/netlify`) | ||
* Save the application, and copy the value for 'Client ID' as the `NETLIFY_CLIENT_ID` and the 'Client Secret' as the `NETLIFY_CLIENT_SECRET` into your `.env.local` file within the project | ||
* If you're testing this on a deployed Netlify site you'll need to set the environment variables as part of the `Site Settings > Build & Deploy > Environment` settings. You'll also need to generate a `NEXTAUTH_SECRET` environment variable and set that for a production build. | ||
|
||
For configuring additional authentication providers, see the original documentation [here](https://github.com/nextauthjs/next-auth-example#3-configure-authentication-providers) |
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 @@ | ||
import { signIn } from "next-auth/react" | ||
|
||
export default function AccessDenied() { | ||
return ( | ||
<> | ||
<h1>Access Denied</h1> | ||
<p> | ||
<a | ||
href="/api/auth/signin" | ||
onClick={(e) => { | ||
e.preventDefault() | ||
signIn() | ||
}} | ||
> | ||
You must be signed in to view this page | ||
</a> | ||
</p> | ||
</> | ||
) | ||
} |
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,14 @@ | ||
.footer { | ||
margin-top: 2rem; | ||
} | ||
|
||
.navItems { | ||
margin-bottom: 1rem; | ||
padding: 0; | ||
list-style: none; | ||
} | ||
|
||
.navItem { | ||
display: inline-block; | ||
margin-right: 1rem; | ||
} |
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,30 @@ | ||
import Link from "next/link" | ||
import styles from "./footer.module.css" | ||
import packageJSON from "../package.json" | ||
|
||
export default function Footer() { | ||
return ( | ||
<footer className={styles.footer}> | ||
<hr /> | ||
<ul className={styles.navItems}> | ||
<li className={styles.navItem}> | ||
<a href="https://next-auth.js.org">Documentation</a> | ||
</li> | ||
<li className={styles.navItem}> | ||
<a href="https://www.npmjs.com/package/next-auth">NPM</a> | ||
</li> | ||
<li className={styles.navItem}> | ||
<a href="https://github.com/nextauthjs/next-auth-example">GitHub</a> | ||
</li> | ||
<li className={styles.navItem}> | ||
<Link href="/policy"> | ||
<a>Policy</a> | ||
</Link> | ||
</li> | ||
<li className={styles.navItem}> | ||
<em>next-auth@{packageJSON.dependencies["next-auth"]}</em> | ||
</li> | ||
</ul> | ||
</footer> | ||
) | ||
} |
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,92 @@ | ||
/* Set min-height to avoid page reflow while session loading */ | ||
.signedInStatus { | ||
display: block; | ||
min-height: 4rem; | ||
width: 100%; | ||
} | ||
|
||
.loading, | ||
.loaded { | ||
position: relative; | ||
top: 0; | ||
opacity: 1; | ||
overflow: hidden; | ||
border-radius: 0 0 0.6rem 0.6rem; | ||
padding: 0.6rem 1rem; | ||
margin: 0; | ||
background-color: rgba(0, 0, 0, 0.05); | ||
transition: all 0.2s ease-in; | ||
} | ||
|
||
.loading { | ||
top: -2rem; | ||
opacity: 0; | ||
} | ||
|
||
.signedInText, | ||
.notSignedInText { | ||
position: absolute; | ||
padding-top: 0.8rem; | ||
left: 1rem; | ||
right: 6.5rem; | ||
white-space: nowrap; | ||
text-overflow: ellipsis; | ||
overflow: hidden; | ||
display: inherit; | ||
z-index: 1; | ||
line-height: 1.3rem; | ||
} | ||
|
||
.signedInText { | ||
padding-top: 0rem; | ||
left: 4.6rem; | ||
} | ||
|
||
.avatar { | ||
border-radius: 2rem; | ||
float: left; | ||
height: 2.8rem; | ||
width: 2.8rem; | ||
background-color: white; | ||
background-size: cover; | ||
background-repeat: no-repeat; | ||
} | ||
|
||
.button, | ||
.buttonPrimary { | ||
float: right; | ||
margin-right: -0.4rem; | ||
font-weight: 500; | ||
border-radius: 0.3rem; | ||
cursor: pointer; | ||
font-size: 1rem; | ||
line-height: 1.4rem; | ||
padding: 0.7rem 0.8rem; | ||
position: relative; | ||
z-index: 10; | ||
background-color: transparent; | ||
color: #555; | ||
} | ||
|
||
.buttonPrimary { | ||
background-color: #346df1; | ||
border-color: #346df1; | ||
color: #fff; | ||
text-decoration: none; | ||
padding: 0.7rem 1.4rem; | ||
} | ||
|
||
.buttonPrimary:hover { | ||
box-shadow: inset 0 0 5rem rgba(0, 0, 0, 0.2); | ||
} | ||
|
||
.navItems { | ||
margin-bottom: 2rem; | ||
padding: 0; | ||
list-style: none; | ||
} | ||
|
||
.navItem { | ||
display: inline-block; | ||
margin-right: 1rem; | ||
} |
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,108 @@ | ||
import Link from "next/link" | ||
import { signIn, signOut, useSession } from "next-auth/react" | ||
import styles from "./header.module.css" | ||
|
||
// The approach used in this component shows how to build a sign in and sign out | ||
// component that works on pages which support both client and server side | ||
// rendering, and avoids any flash incorrect content on initial page load. | ||
export default function Header() { | ||
const { data: session, status } = useSession() | ||
const loading = status === "loading" | ||
|
||
return ( | ||
<header> | ||
<noscript> | ||
<style>{`.nojs-show { opacity: 1; top: 0; }`}</style> | ||
</noscript> | ||
<div className={styles.signedInStatus}> | ||
<p | ||
className={`nojs-show ${ | ||
!session && loading ? styles.loading : styles.loaded | ||
}`} | ||
> | ||
{!session && ( | ||
<> | ||
<span className={styles.notSignedInText}> | ||
You are not signed in | ||
</span> | ||
<a | ||
href={`/api/auth/signin`} | ||
className={styles.buttonPrimary} | ||
onClick={(e) => { | ||
e.preventDefault() | ||
signIn() | ||
}} | ||
> | ||
Sign in | ||
</a> | ||
</> | ||
)} | ||
{session?.user && ( | ||
<> | ||
{session.user.image && ( | ||
<span | ||
style={{ backgroundImage: `url('${session.user.image}')` }} | ||
className={styles.avatar} | ||
/> | ||
)} | ||
<span className={styles.signedInText}> | ||
<small>Signed in as</small> | ||
<br /> | ||
<strong>{session.user.email ?? session.user.name}</strong> | ||
</span> | ||
<a | ||
href={`/api/auth/signout`} | ||
className={styles.button} | ||
onClick={(e) => { | ||
e.preventDefault() | ||
signOut() | ||
}} | ||
> | ||
Sign out | ||
</a> | ||
</> | ||
)} | ||
</p> | ||
</div> | ||
<nav> | ||
<ul className={styles.navItems}> | ||
<li className={styles.navItem}> | ||
<Link href="/"> | ||
<a>Home</a> | ||
</Link> | ||
</li> | ||
<li className={styles.navItem}> | ||
<Link href="/client"> | ||
<a>Client</a> | ||
</Link> | ||
</li> | ||
<li className={styles.navItem}> | ||
<Link href="/server"> | ||
<a>Server</a> | ||
</Link> | ||
</li> | ||
<li className={styles.navItem}> | ||
<Link href="/protected"> | ||
<a>Protected</a> | ||
</Link> | ||
</li> | ||
<li className={styles.navItem}> | ||
<Link href="/api-example"> | ||
<a>API</a> | ||
</Link> | ||
</li> | ||
<li className={styles.navItem}> | ||
<Link href="/admin"> | ||
<a>Admin</a> | ||
</Link> | ||
</li> | ||
<li className={styles.navItem}> | ||
<Link href="/me"> | ||
<a>Me</a> | ||
</Link> | ||
</li> | ||
</ul> | ||
</nav> | ||
</header> | ||
) | ||
} |
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,17 @@ | ||
import Header from "./header" | ||
import Footer from "./footer" | ||
import type { ReactChildren } from "react" | ||
|
||
interface Props { | ||
children: React.ReactNode | ||
} | ||
|
||
export default function Layout({ children }: Props) { | ||
return ( | ||
<> | ||
<Header /> | ||
<main>{children}</main> | ||
<Footer /> | ||
</> | ||
) | ||
} |
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,18 @@ | ||
[build] | ||
command = "next build" | ||
publish = ".next" | ||
ignore = "git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF . ../../plugin" | ||
|
||
[build.environment] | ||
TERM = "xterm" | ||
NODE_VERSION = "17" | ||
ericapisani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[dev] | ||
framework = "#static" | ||
|
||
[[plugins]] | ||
package = "../plugin-wrapper/" | ||
|
||
# This is a fake plugin, that makes it run npm install | ||
[[plugins]] | ||
package = "@netlify/plugin-local-install-core" |
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,10 @@ | ||
import "next-auth/jwt" | ||
|
||
// Read more at: https://next-auth.js.org/getting-started/typescript#module-augmentation | ||
|
||
declare module "next-auth/jwt" { | ||
interface JWT { | ||
/** The user's role. */ | ||
userRole?: "admin" | ||
} | ||
} |
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,5 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
Oops, something went wrong.
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.