Skip to content

Commit

Permalink
Example: add NextAuth example (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang authored Apr 18, 2023
1 parent dc3a892 commit fe6740b
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-trains-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

Example: add NextAuth example
6 changes: 6 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ yarn-error.log*
.env.development.local
.env.test.local
.env.production.local

# open-next
/.open-next

# sst
/.sst
5 changes: 3 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
"date-fns": "^2.23.0",
"gray-matter": "^4.0.3",
"next": "13.0.5",
"next-auth": "^4.22.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"remark": "^13.0.0",
"remark-html": "^13.0.2",
"swr": "^1.0.1"
},
"devDependencies": {
"sst": "^2.5.3",
"aws-cdk-lib": "2.72.1",
"constructs": "10.1.156"
"constructs": "10.1.156",
"sst": "^2.5.3"
}
}
12 changes: 10 additions & 2 deletions example/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import "../styles/global.css";
import { SessionProvider } from "next-auth/react";

export default function App({ Component, pageProps }) {
return <Component {...pageProps} />;
export default function App({
Component,
pageProps: { session, ...pageProps },
}) {
return (
<SessionProvider session={session}>
<Component {...pageProps} />
</SessionProvider>
);
}
14 changes: 14 additions & 0 deletions example/pages/api/auth/[...nextauth].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import NextAuth from "next-auth";
import GithubProvider from "next-auth/providers/github";
import { Config } from "sst/node/config";

export const authOptions = {
providers: [
GithubProvider({
clientId: Config.GITHUB_CLIENT_ID,
clientSecret: Config.GITHUB_CLIENT_SECRET,
}),
],
};

export default NextAuth(authOptions);
1 change: 1 addition & 0 deletions example/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default function Home({}) {
<Link href={`/middleware-redirect`}>Middleware — redirect</Link><br />
<Link href={`/middleware-set-header`}>Middleware — set header</Link><br />
<Link href={`/middleware-geolocation`}>Middleware — geolocation</Link><br />
<Link href={`/next-auth`}>NextAuth</Link><br />
<Link href={`/image-optimization-imported`}>Image Optimization — imported image</Link><br />
<Link href={`/image-optimization-remote`}>Image Optimization — remote image</Link><br />
<Link href={`/image-html-tag`}>Image using html image tag</Link><br />
Expand Down
41 changes: 41 additions & 0 deletions example/pages/next-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Layout from "../components/layout";
import Link from "next/link";
import { useSession, signIn, signOut } from "next-auth/react";

function LoginButton() {
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>
</>
);
}

export default function Page() {
return (
<Layout>
<article>
<h1>
NextAuth
</h1>
<hr />
<LoginButton />
<br />
<p>
<b>Test 1:</b> Sign in, and your email is displayed.
<br />
<b>Test 2:</b> Sign out, and your email is cleared.
</p>
</article>
</Layout>
);
}
103 changes: 95 additions & 8 deletions example/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions example/sst.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SSTConfig } from "sst";
import { NextjsSite } from "sst/constructs";
import { Config, NextjsSite } from "sst/constructs";

export default {
config(_input) {
Expand All @@ -10,7 +10,12 @@ export default {
},
stacks(app) {
app.stack(function Site({ stack }) {
const site = new NextjsSite(stack, "site");
const site = new NextjsSite(stack, "site",{
bind: [
new Config.Secret(stack, "GITHUB_CLIENT_ID"),
new Config.Secret(stack, "GITHUB_CLIENT_SECRET"),
],
});

stack.addOutputs({
SiteUrl: site.url,
Expand Down

1 comment on commit fe6740b

@vercel
Copy link

@vercel vercel bot commented on fe6740b Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

open-next – ./

open-next-sst-dev.vercel.app
open-next-git-main-sst-dev.vercel.app
open-next.vercel.app

Please sign in to comment.