Skip to content

Commit e61e003

Browse files
authored
Merge pull request #97 from thefrontside/feat/auth0-example
feat: auth0 service examples
2 parents 07c79ad + 2657e48 commit e61e003

38 files changed

+21584
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@simulacrum-examples/nextjs-with-auth0-react": minor
3+
---
4+
5+
Add example using nextjs and auth0-react making use of the auth0 simulator.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@simulacrum-examples/nextjs-with-nextjs-auth0": patch
3+
---
4+
5+
Add example using nextjs and nextjs-auth0 making use of the auth0 simulator.

.changes/config.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@
2626
"path": "./packages/ui",
2727
"manager": "javascript",
2828
"dependencies": []
29+
},
30+
"@simulacrum-examples/nextjs-with-auth0-react": {
31+
"path": "./examples/nextjs-with-auth0-react",
32+
"manager": "javascript",
33+
"dependencies": [
34+
"@simulacrum/auth0",
35+
"@simulacrum/server",
36+
"@simulacrum/client"
37+
],
38+
"publish": false,
39+
"releaseTag": false
40+
},
41+
"@simulacrum-examples/nextjs-with-nextjs-auth0": {
42+
"path": "./examples/nextjs-with-nextjs-auth0",
43+
"manager": "javascript",
44+
"dependencies": [
45+
"@simulacrum/auth0",
46+
"@simulacrum/server",
47+
"@simulacrum/client"
48+
],
49+
"publish": false,
50+
"releaseTag": false
2951
}
3052
}
3153
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ is just connects over HTTP and so can be used from any language.
3232

3333
To create a simulation in a simulacrum server with one of its
3434
available simulators. In this case, we'll assume that there is an
35-
`auth0` simualtor on the server that we can use to create a simulation.
35+
`auth0` simulator on the server that we can use to create a simulation.
3636

3737

3838
``` javascript
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Anything prepended with NEXT_PUBLIC_ is available client side
2+
3+
# A long, secret value used to encrypt the session cookie
4+
AUTH0_SECRET='6d0598f28f62a9aee14929ef46c7c8befdc0150d870ec462fa45629511fd2a46'
5+
# The base url of your application, the nextjs dev server
6+
NEXT_PUBLIC_AUTH0_BASE_URL='localhost:3000'
7+
# The url of your Auth0 tenant domain, we point at the simulacrum auth0 service
8+
NEXT_PUBLIC_AUTH0_ISSUER_BASE_URL='localhost:4400'
9+
# Your Auth0 application's Client ID
10+
NEXT_PUBLIC_AUTH0_CLIENT_ID='YOUR_AUTH0_CLIENT_ID'
11+
# Your Auth0 application's Client Secret
12+
AUTH0_CLIENT_SECRET='YOUR_AUTH0_CLIENT_SECRET'
13+
# Audience
14+
NEXT_PUBLIC_AUTH0_AUDIENCE='https://your-audience/'
15+
# Scope
16+
NEXT_PUBLIC_AUTH0_SCOPE='read:users'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env.local
29+
.env.development.local
30+
.env.test.local
31+
.env.production.local
32+
33+
# vercel
34+
.vercel
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Simulacrum Server with Auth0 Interfaced With Nextjs
2+
3+
[![Edit remark-codesandbox demo](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/thefrontside/simulacrum/tree/v0/examples/nextjs_with_auth0-react)
4+
5+
This uses the [auth0/auth0-react](https://github.com/auth0/auth0-react) package. It is setup using those instructions with the `domain` pointing at our simulacrum service instead of the Auth0 API. This greatly improves the experience around testing locally, and then changing an environment variable to test in "staging" or moving to "production".
6+
7+
## Getting Started
8+
9+
First, run the simulacrum server with the auth0 service. Note this example and lockfile use `npm@7`. It will start up the service and create a user for you to use to login.
10+
11+
```bash
12+
npm run sim
13+
```
14+
15+
In a separate terminal window, also run the development server:
16+
17+
```bash
18+
npm run dev
19+
```
20+
21+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
22+
23+
Click on the login link, and enter the information from the user created in your simulation. It is output in your console running the simulation.
24+
25+
Now you are logged in!
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React from "react";
2+
import Link from "next/link";
3+
import { useAuth0 } from "@auth0/auth0-react";
4+
5+
const Header = (): React.ReactElement => {
6+
let { user, loginWithRedirect, logout } = useAuth0();
7+
8+
return (
9+
<header>
10+
<nav>
11+
<ul>
12+
<li>
13+
<Link href="/">
14+
<a>Home</a>
15+
</Link>
16+
</li>
17+
{user ? (
18+
<>
19+
<li>
20+
<button
21+
onClick={() => logout({ returnTo: window.location.origin })}
22+
>
23+
Log out
24+
</button>
25+
</li>
26+
</>
27+
) : (
28+
<>
29+
<li>
30+
<button onClick={loginWithRedirect}>Login</button>
31+
</li>
32+
</>
33+
)}
34+
</ul>
35+
</nav>
36+
37+
<style jsx>{`
38+
header {
39+
padding: 0.2rem;
40+
color: #fff;
41+
background-color: #333;
42+
}
43+
nav {
44+
max-width: 42rem;
45+
margin: 1.5rem auto;
46+
}
47+
ul {
48+
display: flex;
49+
list-style: none;
50+
margin-left: 0;
51+
padding-left: 0;
52+
}
53+
li {
54+
margin-right: 1rem;
55+
}
56+
li:nth-child(3) {
57+
margin-right: auto;
58+
}
59+
a {
60+
color: #fff;
61+
text-decoration: none;
62+
}
63+
button {
64+
font-size: 1rem;
65+
color: #fff;
66+
cursor: pointer;
67+
border: none;
68+
background: none;
69+
}
70+
`}</style>
71+
</header>
72+
);
73+
};
74+
75+
export default Header;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React from "react";
2+
import Head from "next/head";
3+
4+
import Header from "./header";
5+
6+
const Layout = ({
7+
children,
8+
}: React.PropsWithChildren<unknown>): React.ReactElement => (
9+
<>
10+
<Head>
11+
<title>Next.js with Auth0 Simulacrum Server</title>
12+
</Head>
13+
14+
<Header />
15+
16+
<main>
17+
<div className="container">{children}</div>
18+
</main>
19+
20+
<style jsx>{`
21+
.container {
22+
max-width: 42rem;
23+
margin: 1.5rem auto;
24+
}
25+
`}</style>
26+
<style jsx global>{`
27+
body {
28+
margin: 0;
29+
color: #333;
30+
font-family: -apple-system, "Segoe UI";
31+
}
32+
`}</style>
33+
</>
34+
);
35+
36+
export default Layout;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/types/global" />
3+
/// <reference types="next/image-types/global" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
reactStrictMode: true,
3+
};

0 commit comments

Comments
 (0)