Skip to content

Commit

Permalink
add nextjs app
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdstock committed May 4, 2020
1 parent 4d9ac15 commit 4372ebc
Show file tree
Hide file tree
Showing 16 changed files with 11,642 additions and 3 deletions.
3 changes: 3 additions & 0 deletions examples/next-js/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
AUTH0_DOMAIN=
AUTH0_CLIENT_ID=
AUTH0_CALLBACK_URL=http://localhost:3000/auth/callback
30 changes: 30 additions & 0 deletions examples/next-js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local
30 changes: 30 additions & 0 deletions examples/next-js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/zeit/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/zeit/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/import?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
2 changes: 2 additions & 0 deletions examples/next-js/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
8 changes: 8 additions & 0 deletions examples/next-js/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require("dotenv").config();
module.exports = {
env: {
AUTH0_DOMAIN: process.env.AUTH0_DOMAIN,
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
AUTH0_REDIRECT_URI: process.env.AUTH0_REDIRECT_URI,
},
};
18 changes: 18 additions & 0 deletions examples/next-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "next-example",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"@react-client-auth/auth0": "^1.0.1",
"@react-client-auth/core": "^1.0.0",
"dotenv": "^8.2.0",
"next": "9.3.6",
"react": "16.13.1",
"react-dom": "16.13.1"
}
}
14 changes: 14 additions & 0 deletions examples/next-js/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from "react";
import { SessionProvider } from "@react-client-auth/core";
import { Auth0Service } from "@react-client-auth/auth0";

const App = ({ Component, pageProps }) => {
const auth = new Auth0Service();
return (
<SessionProvider auth={auth}>
<Component {...pageProps} />
</SessionProvider>
);
};

export default App;
6 changes: 6 additions & 0 deletions examples/next-js/pages/api/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction

export default (req, res) => {
res.statusCode = 200
res.json({ name: 'John Doe' })
}
34 changes: 34 additions & 0 deletions examples/next-js/pages/auth/callback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as React from "react";
import { useRouter } from "next/router";
import { useAuth } from "@react-client-auth/core";
import { NextPage } from "next";

const postLogin = (router) => {
const postLoginUrl = localStorage.getItem("postLoginUrl");
localStorage.removeItem("postLoginUrl");
router.push(postLoginUrl || "/");
};

const CallbackPage: NextPage = () => {
const session = useAuth();
const router = useRouter();

React.useEffect(() => {
if (/access_token|id_token|error/.test(location.hash)) {
session.auth
.handleAuthentication()
.then((user) => {
session.setUser(user);
postLogin(router);
})
.catch((error) => {
console.warn("auth0 authentication rejected", error);
router.push("/");
});
}
}, []);

return <pre>Loading ...</pre>;
};

export default CallbackPage;
201 changes: 201 additions & 0 deletions examples/next-js/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import Head from "next/head";
import { useAuth, Session } from "@react-client-auth/core";

const SessionButton: React.FC<{ session: Session }> = ({ session }) => {
return session.user.isLoggedIn ? (
<a onClick={() => session.auth.logout()}>Log Out</a>
) : (
<a onClick={() => session.auth.authorize()}>Sign In</a>
);
};

export default function Home() {
const session = useAuth();
const { user } = session;
return (
<div className="container">
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<main>
<h1 className="title">React Client Auth Demo</h1>
<pre>
<a href="https://github.com/erikdstock/react-client-auth">
Repository
</a>
</pre>

<SessionButton session={session} />

<div className="grid">
{user.isLoggedIn && (
<div className="card">
<h3>Auth0 Profile</h3>
<img src={user.profile.picture} />
<p>{user.profile.nickname}</p>
</div>
)}
</div>
</main>

<footer>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{" "}
<img src="/vercel.svg" alt="Vercel Logo" className="logo" />
</a>
</footer>

<style jsx>{`
.container {
min-height: 100vh;
padding: 0 0.5rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
main {
padding: 5rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
footer {
width: 100%;
height: 100px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
}
footer img {
margin-left: 0.5rem;
}
footer a {
display: flex;
justify-content: center;
align-items: center;
}
a {
color: inherit;
text-decoration: none;
}
.title a {
color: #0070f3;
text-decoration: none;
}
.title a:hover,
.title a:focus,
.title a:active {
text-decoration: underline;
}
.title {
margin: 0;
line-height: 1.15;
font-size: 4rem;
}
.title,
.description {
text-align: center;
}
.description {
line-height: 1.5;
font-size: 1.5rem;
}
code {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
font-size: 1.1rem;
font-family: Menlo, Monaco, Lucida Console, Liberation Mono,
DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace;
}
.grid {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 800px;
margin-top: 3rem;
}
.card {
margin: 1rem;
flex-basis: 45%;
padding: 1.5rem;
text-align: left;
color: inherit;
text-decoration: none;
border: 1px solid #eaeaea;
border-radius: 10px;
transition: color 0.15s ease, border-color 0.15s ease;
}
.card:hover,
.card:focus,
.card:active {
color: #0070f3;
border-color: #0070f3;
}
.card h3 {
margin: 0 0 1rem 0;
font-size: 1.5rem;
}
.card p {
margin: 0;
font-size: 1.25rem;
line-height: 1.5;
}
.logo {
height: 1em;
}
@media (max-width: 600px) {
.grid {
width: 100%;
flex-direction: column;
}
}
`}</style>

<style jsx global>{`
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto,
Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue,
sans-serif;
}
* {
box-sizing: border-box;
}
`}</style>
</div>
);
}
Binary file added examples/next-js/public/favicon.ico
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/next-js/public/vercel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions examples/next-js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"exclude": [
"node_modules"
],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
]
}
Loading

0 comments on commit 4372ebc

Please sign in to comment.