Skip to content

Commit fe6740b

Browse files
authored
Example: add NextAuth example (#91)
1 parent dc3a892 commit fe6740b

File tree

9 files changed

+182
-14
lines changed

9 files changed

+182
-14
lines changed

.changeset/grumpy-trains-dream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
Example: add NextAuth example

example/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ yarn-error.log*
2828
.env.development.local
2929
.env.test.local
3030
.env.production.local
31+
32+
# open-next
33+
/.open-next
34+
35+
# sst
36+
/.sst

example/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
"date-fns": "^2.23.0",
1212
"gray-matter": "^4.0.3",
1313
"next": "13.0.5",
14+
"next-auth": "^4.22.0",
1415
"react": "18.2.0",
1516
"react-dom": "18.2.0",
1617
"remark": "^13.0.0",
1718
"remark-html": "^13.0.2",
1819
"swr": "^1.0.1"
1920
},
2021
"devDependencies": {
21-
"sst": "^2.5.3",
2222
"aws-cdk-lib": "2.72.1",
23-
"constructs": "10.1.156"
23+
"constructs": "10.1.156",
24+
"sst": "^2.5.3"
2425
}
2526
}

example/pages/_app.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import "../styles/global.css";
2+
import { SessionProvider } from "next-auth/react";
23

3-
export default function App({ Component, pageProps }) {
4-
return <Component {...pageProps} />;
4+
export default function App({
5+
Component,
6+
pageProps: { session, ...pageProps },
7+
}) {
8+
return (
9+
<SessionProvider session={session}>
10+
<Component {...pageProps} />
11+
</SessionProvider>
12+
);
513
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import NextAuth from "next-auth";
2+
import GithubProvider from "next-auth/providers/github";
3+
import { Config } from "sst/node/config";
4+
5+
export const authOptions = {
6+
providers: [
7+
GithubProvider({
8+
clientId: Config.GITHUB_CLIENT_ID,
9+
clientSecret: Config.GITHUB_CLIENT_SECRET,
10+
}),
11+
],
12+
};
13+
14+
export default NextAuth(authOptions);

example/pages/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default function Home({}) {
3535
<Link href={`/middleware-redirect`}>Middleware — redirect</Link><br />
3636
<Link href={`/middleware-set-header`}>Middleware — set header</Link><br />
3737
<Link href={`/middleware-geolocation`}>Middleware — geolocation</Link><br />
38+
<Link href={`/next-auth`}>NextAuth</Link><br />
3839
<Link href={`/image-optimization-imported`}>Image Optimization — imported image</Link><br />
3940
<Link href={`/image-optimization-remote`}>Image Optimization — remote image</Link><br />
4041
<Link href={`/image-html-tag`}>Image using html image tag</Link><br />

example/pages/next-auth.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Layout from "../components/layout";
2+
import Link from "next/link";
3+
import { useSession, signIn, signOut } from "next-auth/react";
4+
5+
function LoginButton() {
6+
const { data: session } = useSession();
7+
if (session) {
8+
return (
9+
<>
10+
Signed in as {session.user.email} <br />
11+
<button onClick={() => signOut()}>Sign out</button>
12+
</>
13+
);
14+
}
15+
return (
16+
<>
17+
Not signed in <br />
18+
<button onClick={() => signIn()}>Sign in</button>
19+
</>
20+
);
21+
}
22+
23+
export default function Page() {
24+
return (
25+
<Layout>
26+
<article>
27+
<h1>
28+
NextAuth
29+
</h1>
30+
<hr />
31+
<LoginButton />
32+
<br />
33+
<p>
34+
<b>Test 1:</b> Sign in, and your email is displayed.
35+
<br />
36+
<b>Test 2:</b> Sign out, and your email is cleared.
37+
</p>
38+
</article>
39+
</Layout>
40+
);
41+
}

example/pnpm-lock.yaml

Lines changed: 95 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/sst.config.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SSTConfig } from "sst";
2-
import { NextjsSite } from "sst/constructs";
2+
import { Config, NextjsSite } from "sst/constructs";
33

44
export default {
55
config(_input) {
@@ -10,7 +10,12 @@ export default {
1010
},
1111
stacks(app) {
1212
app.stack(function Site({ stack }) {
13-
const site = new NextjsSite(stack, "site");
13+
const site = new NextjsSite(stack, "site",{
14+
bind: [
15+
new Config.Secret(stack, "GITHUB_CLIENT_ID"),
16+
new Config.Secret(stack, "GITHUB_CLIENT_SECRET"),
17+
],
18+
});
1419

1520
stack.addOutputs({
1621
SiteUrl: site.url,

0 commit comments

Comments
 (0)