/
index.js
295 lines (283 loc) · 9.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import React, { useEffect } from "react"
import classnames from "classnames"
import Layout from "@theme/Layout"
import Link from "@docusaurus/Link"
import useDocusaurusContext from "@docusaurus/useDocusaurusContext"
import useBaseUrl from "@docusaurus/useBaseUrl"
import CodeBlock from "@theme/CodeBlock"
import ProviderMarquee from "../components/ProviderMarquee"
import Seo from "./seo"
import styles from "./index.module.css"
const features = [
{
title: "Easy",
imageUrl: "img/undraw_social.svg",
description: (
<ul>
<li>
Built in support for popular services
<br />
<em>(Google, Facebook, Auth0, Apple…)</em>
</li>
<li>Built in email / passwordless / magic link</li>
<li>Use with any username / password store</li>
<li>Use with OAuth 1.0 & 2.0 services</li>
</ul>
),
},
{
title: "Flexible",
imageUrl: "img/undraw_authentication.svg",
description: (
<ul>
<li>Built for Serverless, runs anywhere</li>
<li>
Bring Your Own Database - or none!
<br />
<em>(MySQL, Postgres, MSSQL, MongoDB…)</em>
</li>
<li>Choose database sessions or JWT</li>
<li>Secure web pages and API routes</li>
</ul>
),
},
{
title: "Secure",
imageUrl: "img/undraw_secure.svg",
description: (
<ul>
<li>Signed, prefixed, server-only cookies</li>
<li>HTTP POST + CSRF Token validation</li>
<li>JWT with JWS / JWE / JWK</li>
<li>Tab syncing, auto-revalidation, keepalives</li>
<li>Doesn't rely on client side JavaScript</li>
</ul>
),
},
]
const kFormatter = (num) => {
return Math.sign(num) * (Math.abs(num) / 1000).toFixed(1) + "k"
}
function Feature({ imageUrl, title, description }) {
const imgUrl = useBaseUrl(imageUrl)
return (
<div className={classnames("col col--4", styles.feature)}>
{imgUrl && (
<div className="text--center">
<div className="feature-image-wrapper">
<img className={styles.featureImage} src={imgUrl} alt={title} />
</div>
</div>
)}
<h3 className="text--center">{title}</h3>
<div>{description}</div>
</div>
)
}
function Home() {
const context = useDocusaurusContext()
const { siteConfig = {} } = context
useEffect(() => {
window
.fetch("https://api.github.com/repos/nextauthjs/next-auth")
.then((res) => res.json())
.then((data) => {
const navLinks = document.getElementsByClassName(
"navbar__item navbar__link"
)
const githubStat = document.createElement("span")
githubStat.innerHTML = kFormatter(data.stargazers_count)
githubStat.className = "github-counter"
navLinks[4].appendChild(githubStat)
})
}, [])
return (
<Layout description={siteConfig.tagline}>
<Seo />
<div className="home-wrapper">
<header className={classnames("hero", styles.heroBanner)}>
<div className="container">
<div className="hero-inner">
<img
src="/img/logo/logo-sm.png"
alt="Shield with key icon"
className={styles.heroLogo}
/>
<div className={styles.heroText}>
<h1 className="hero__title">{siteConfig.title}</h1>
<p className="hero__subtitle">{siteConfig.tagline}</p>
</div>
<div className={styles.buttons}>
<a
className={classnames(
"button button--outline button--secondary button--lg rounded-pill",
styles.button
)}
href="https://next-auth-example.vercel.app"
>
Live Demo
</a>
<Link
className={classnames(
"button button--primary button--lg rounded-pill",
styles.button
)}
to={useBaseUrl("/getting-started/example")}
>
Get Started
</Link>
</div>
</div>
<div className="hero-marquee">
<ProviderMarquee />
</div>
</div>
<div className="hero-wave">
<div className="hero-wave-inner" />
</div>
</header>
<main className="home-main">
<section className={`section-features ${styles.features}`}>
<div className="container">
<div className="row">
<div className="col">
<h2 className={styles.featuresTitle}>
<span>Open Source.</span> <span>Full Stack.</span>{" "}
<span>Own Your Data.</span>
</h2>
</div>
</div>
<div className="row">
{features.map((props, idx) => (
<Feature key={idx} {...props} />
))}
</div>
</div>
</section>
<section>
<div className="container">
<div className="row">
<div className="col">
<p className="text--center">
<a
href="https://www.npmjs.com/package/next-auth"
className="button button--primary button--outline rounded-pill button--lg"
>
npm install next-auth
</a>
</p>
</div>
</div>
<div className="row">
<div className="col">
<h2 className="text--center" style={{ fontSize: "2.5rem" }}>
Add authentication in minutes!
</h2>
</div>
</div>
<div className="row">
<div className="col col--6">
<div className="code">
<h4 className="code-heading">
Server <span>/pages/api/auth/[...nextauth].js</span>
</h4>
<CodeBlock className="prism-code language-js">
{serverlessFunctionCode}
</CodeBlock>
</div>
</div>
<div className="col col--6">
<div className="code">
<h4 className="code-heading">
Client (App) <span>/pages/_app.jsx</span>
</h4>
<CodeBlock className="prism-code language-js">
{appCode}
</CodeBlock>
<h4 className="code-heading">
Client (Page) <span>/pages/index.js</span>
</h4>
<CodeBlock className="prism-code language-js">
{pageCode}
</CodeBlock>
</div>
</div>
</div>
<div className="row">
<div className="col">
<p className="text--center" style={{ marginTop: "2rem" }}>
<Link
to="/getting-started/example"
className="button button--primary button--lg rounded-pill"
>
Example Code
</Link>
</p>
</div>
</div>
</div>
</section>
<div className={styles.homeSubtitle}>
<p>NextAuth.js is an open source community project.</p>
</div>
</main>
</div>
</Layout>
)
}
const appCode = `
import { SessionProvider } from "next-auth/react"
export default function App({
Component, pageProps: { session, ...pageProps }
}) {
return (
<SessionProvider session={session}>
<Component {...pageProps}/>
</SessionProvider>
)
}`.trim()
const pageCode = `
import { useSession, signIn, signOut } from "next-auth/react"
export default function Component() {
const { data: session } = useSession()
if(session) {
return <>
Signed in as {session.user.email} <br/>
<button onClick={() => signOut()}>Sign out</button>
</>
}
return <>
Not signed in <br/>
<button onClick={() => signIn()}>Sign in</button>
</>
}`.trim()
const serverlessFunctionCode = `
import NextAuth from 'next-auth'
import AppleProvider from 'next-auth/providers/apple'
import FacebookProvider from 'next-auth/providers/facebook'
import GoogleProvider from 'next-auth/providers/google'
import EmailProvider from 'next-auth/providers/email'
export default NextAuth({
providers: [
// OAuth authentication providers...
AppleProvider({
clientId: process.env.APPLE_ID,
clientSecret: process.env.APPLE_SECRET
}),
FacebookProvider({
clientId: process.env.FACEBOOK_ID,
clientSecret: process.env.FACEBOOK_SECRET
}),
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET
}),
// Passwordless / email sign in
EmailProvider({
server: process.env.MAIL_SERVER,
from: 'NextAuth.js <no-reply@example.com>'
}),
]
})
`.trim()
export default Home