Skip to content

Commit 885dfca

Browse files
committed
Initial commit
0 parents  commit 885dfca

24 files changed

+4717
-0
lines changed

.env.local.example

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
NEXTAUTH_URL=http://localhost:3000
2+
SECRET= # Linux: `openssl rand -hex 32` or go to https://generate-secret.now.sh/32
3+
4+
APPLE_ID=
5+
APPLE_TEAM_ID=
6+
APPLE_PRIVATE_KEY=
7+
APPLE_KEY_ID=
8+
9+
AUTH0_ID=
10+
AUTH0_SECRET=
11+
AUTH0_DOMAIN=
12+
13+
FACEBOOK_ID=
14+
FACEBOOK_SECRET=
15+
16+
GITHUB_ID=
17+
GITHUB_SECRET=
18+
19+
GOOGLE_ID=
20+
GOOGLE_SECRET=
21+
22+
TWITTER_ID=
23+
TWITTER_SECRET=
24+
25+
EMAIL_SERVER=smtp://username:[email protected]:587
26+
EMAIL_FROM=NextAuth <[email protected]>
27+
28+
DATABASE_URL=sqlite://localhost/:memory:?synchronize=true

.gitignore

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# Next.js build output
79+
.next
80+
81+
# Nuxt.js build / generate output
82+
.nuxt
83+
dist
84+
85+
# Gatsby files
86+
.cache/
87+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88+
# https://nextjs.org/blog/next-9-1#public-directory-support
89+
# public
90+
91+
# vuepress build output
92+
.vuepress/dist
93+
94+
# Serverless directories
95+
.serverless/
96+
97+
# FuseBox cache
98+
.fusebox/
99+
100+
# DynamoDB Local files
101+
.dynamodb/
102+
103+
# TernJS port file
104+
.tern-port
105+
106+
.vercel
107+
.now
108+
.env.local
109+
110+
.DS_Store

LICENSE

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ISC License
2+
3+
Copyright (c) 2018-2021, Iain Collins
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README.md

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# NextAuth.js Example
2+
3+
[next-auth-example.now.sh](https://next-auth-example.now.sh)
4+
5+
## About this project
6+
7+
This is an example of how to use [NextAuth.js](https://next-auth.js.org) library to add authentication to a [Next.js](https://nextjs.org) application.
8+
9+
## About NextAuth.js
10+
11+
NextAuth.js is an easy to implement, full-stack (client/server) open source authentication library designed for [Next.js](https://nextjs.org) and [Serverless](https://now.sh).
12+
13+
Go to [next-auth.js.org](https://next-auth.js.org) for more information and documentation.
14+
15+
*NextAuth.js is not associated with Vercel or Next.js.*
16+
17+
## Getting started
18+
19+
### 1. Clone the repository and install dependancies
20+
21+
```
22+
git clone https://github.com/nextauthjs/next-auth-example.git
23+
cd next-auth-example
24+
npm i
25+
```
26+
27+
### 2. Configure your local environment
28+
29+
Copy the .env.local.example file in this directory to .env.local (which will be ignored by Git):
30+
31+
```
32+
cp .env.local.example .env.local
33+
```
34+
35+
Add details for one or more providers (e.g. Google, Twitter, GitHub, Email, etc).
36+
37+
#### Database configuration
38+
39+
A database is needed to persist user accounts and to support email sign in, but you can still use NextAuth.js for authentication without one by using OAuth for authentication. If you do not specify a database, JSON Web Tokens will be enabled by default.
40+
41+
You can skip configuring a database and come back to it later if you want.
42+
43+
When configuring your database you should also install an appropriate node_module.
44+
45+
* **SQLite**
46+
47+
Install module:
48+
`npm i sqlite3`
49+
50+
Database URI:
51+
`sqlite://localhost/:memory:?synchronize=true`
52+
53+
* **MySQL**
54+
55+
Install module:
56+
`npm i mysql`
57+
58+
Database URI:
59+
`mysql://username:[email protected]:3306/database_name?synchronize=true`
60+
61+
* **Postgres**
62+
63+
Install module:
64+
`npm i pg`
65+
66+
Database URI:
67+
`postgres://username:[email protected]:5432/database_name?synchronize=true`
68+
69+
* **MongoDB**
70+
71+
Install module:
72+
`npm i mongodb`
73+
74+
Database URI:
75+
`mongodb://username:[email protected]:27017/database_name?synchronize=true`
76+
77+
Notes:
78+
79+
* The example .env specifies an in-memory SQLite database that does not persist data.
80+
* SQLite is suitable for development / testing but not for production.
81+
* The option `?synchronize=true` automatically syncs schema changes to the database. It should not be used in production as may result in data loss if there are changes to the schema or to NextAuth.js
82+
* You can also specify a [TypeORM connection object](https://typeorm.io/#/connection-options) in `pages/api/auth/[...nextauth].js` instead of a database URL / connection string.
83+
84+
### 3. Configure authentication providers
85+
86+
* Review and update options in `pages/api/auth/[...nextauth].js` as needed.
87+
88+
* When setting up OAuth, in the developer admin page for each of your OAuth services, you should configure the callback URL to use a callback path of `{server}/api/auth/callback/{provider}`.
89+
90+
e.g. For Google OAuth you would use: `http://localhost:3000/api/auth/callback/google`
91+
92+
A list of configured providers and their callback URLs is available from the endpoint `/api/auth/providers`. You can find more information at https://next-auth.js.org/configuration/providers
93+
94+
* You can also choose to specify an SMTP server for passwordless sign in via email.
95+
96+
### 4. Start the application
97+
98+
To run your site locally, use:
99+
100+
```
101+
npm run dev
102+
```
103+
104+
To run it it production mode, use:
105+
106+
```
107+
npm build
108+
npm start
109+
```
110+
111+
### 5. Configuring for production
112+
113+
You must set the NEXTAUTH_URL environment variable with the URL of your site, before deploying to production.
114+
115+
e.g. `NEXTAUTH_URL=https://example.com`
116+
117+
To do this in on Vercel, you can use the [Vercel project dashboard](https://vercel.com/dashboard) or the `vc env` command:
118+
119+
vc env add NEXTAUTH_URL production
120+
121+
Be sure to also set environment variables for the Client ID and Client Secret values for all your authentication providers.
122+

components/access-denied.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { signIn } from 'next-auth/client'
2+
3+
export default function AccessDenied () {
4+
return (
5+
<>
6+
<h1>Access Denied</h1>
7+
<p>
8+
<a href="/api/auth/signin"
9+
onClick={(e) => {
10+
e.preventDefault()
11+
signIn()
12+
}}>You must be signed in to view this page</a>
13+
</p>
14+
</>
15+
)
16+
}

components/footer.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Link from "next/link"
2+
import styles from "./footer.module.css"
3+
import { dependencies } from "../package.json"
4+
5+
export default function Footer() {
6+
return (
7+
<footer className={styles.footer}>
8+
<hr />
9+
<ul className={styles.navItems}>
10+
<li className={styles.navItem}>
11+
<a href="https://next-auth.js.org">Documentation</a>
12+
</li>
13+
<li className={styles.navItem}>
14+
<a href="https://www.npmjs.com/package/next-auth">NPM</a>
15+
</li>
16+
<li className={styles.navItem}>
17+
<a href="https://github.com/nextauthjs/next-auth-example">GitHub</a>
18+
</li>
19+
<li className={styles.navItem}>
20+
<Link href="/policy">
21+
<a>Policy</a>
22+
</Link>
23+
</li>
24+
<li className={styles.navItem}>
25+
<em>next-auth@{dependencies["next-auth"]}</em>
26+
</li>
27+
</ul>
28+
</footer>
29+
)
30+
}

components/footer.module.css

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.footer {
2+
margin-top: 2rem;
3+
}
4+
5+
.navItems {
6+
margin-bottom: 1rem;
7+
padding: 0;
8+
list-style: none;
9+
}
10+
11+
.navItem {
12+
display: inline-block;
13+
margin-right: 1rem;
14+
}

0 commit comments

Comments
 (0)