Skip to content

Commit 6e4f1e2

Browse files
chore(examples): upgrade Auth0 example (#41284)
feat: [...auth0] Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
1 parent 3d499a6 commit 6e4f1e2

17 files changed

+126
-228
lines changed

examples/auth0/.env.local.example

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
# Public Environment variables that can be used in the browser.
2-
NEXT_PUBLIC_AUTH0_CLIENT_ID=TEST
3-
NEXT_PUBLIC_AUTH0_SCOPE="openid profile"
4-
NEXT_PUBLIC_AUTH0_DOMAIN="http://example.com"
5-
NEXT_PUBLIC_BASE_URL="http://localhost:3000"
6-
NEXT_PUBLIC_REDIRECT_URI="/api/callback"
7-
NEXT_PUBLIC_POST_LOGOUT_REDIRECT_URI="/"
8-
9-
# Secret environment variables only available to Node.js
1+
AUTH0_ISSUER_BASE_URL="https://"
2+
AUTH0_CLIENT_ID=
103
AUTH0_CLIENT_SECRET=
11-
SESSION_COOKIE_SECRET=
12-
SESSION_COOKIE_LIFETIME=7200
4+
AUTH0_BASE_URL="http://localhost:3000"
5+
AUTH0_SECRET=

examples/auth0/README.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ This example shows how you can use `@auth0/nextjs-auth` to easily add authentica
66
- Signing out
77
- Loading the user on the server side and adding it as part of SSR ([`pages/advanced/ssr-profile.tsx`](pages/advanced/ssr-profile.tsx))
88
- Loading the user on the client side and using fast/cached SSR pages ([`pages/index.tsx`](pages/index.tsx))
9-
- API Routes which can load the current user ([`pages/api/me.ts`](pages/api/me.ts))
10-
- Using hooks to make the user available throughout the application ([`lib/user.ts`](lib/user.ts))
9+
- Loading the user on the client side and checking authentication CSR pages ([`pages/profile.tsx`](pages/profile.tsx))
10+
- Loading the user on the client side by accessing API (Serverless function) CSR pages ([`pages/advanced/api-profile.tsx`](pages/advanced/api-profile.tsx))
11+
- Creates route handlers under the hood that perform different parts of the authentication flow ([`pages/auth/[...auth0].tsx`](pages/auth/[...auth0].tsx))
1112

1213
Read more: [https://auth0.com/blog/ultimate-guide-nextjs-authentication-auth0/](https://auth0.com/blog/ultimate-guide-nextjs-authentication-auth0/)
1314

@@ -50,14 +51,11 @@ cp .env.local.example .env.local
5051

5152
Then, open `.env.local` and add the missing environment variables:
5253

53-
- `NEXT_PUBLIC_AUTH0_DOMAIN` - Can be found in the Auth0 dashboard under `settings`. (Should be prefixed with `https://`)
54-
- `NEXT_PUBLIC_AUTH0_CLIENT_ID` - Can be found in the Auth0 dashboard under `settings`.
54+
- `AUTH0_ISSUER_BASE_URL` - Can be found in the Auth0 dashboard under `settings`. (Should be prefixed with `https://`)
55+
- `AUTH0_CLIENT_ID` - Can be found in the Auth0 dashboard under `settings`.
5556
- `AUTH0_CLIENT_SECRET` - Can be found in the Auth0 dashboard under `settings`.
56-
- `NEXT_PUBLIC_BASE_URL` - The base url of the application.
57-
- `NEXT_PUBLIC_REDIRECT_URI` - The relative url path where Auth0 redirects back to.
58-
- `NEXT_PUBLIC_POST_LOGOUT_REDIRECT_URI` - Where to redirect after logging out.
59-
- `SESSION_COOKIE_SECRET` - A unique secret used to encrypt the cookies, has to be at least 32 characters. You can use [this generator](https://generate-secret.vercel.app/32) to generate a value.
60-
- `SESSION_COOKIE_LIFETIME` - How long a session lasts in seconds. The default is 2 hours.
57+
- `AUTH0_BASE_URL` - The base url of the application.
58+
- `AUTH0_SECRET` - Has to be at least 32 characters. You can use [this generator](https://generate-secret.vercel.app/32) to generate a value.
6159

6260
## Deploy on Vercel
6361

examples/auth0/components/header.tsx

+10-4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ const Header = ({ user, loading }: HeaderProps) => {
2020
<a>About</a>
2121
</Link>
2222
</li>
23+
<li>
24+
<Link href="/advanced/api-profile">
25+
<a>API rendered profile (advanced)</a>
26+
</Link>
27+
</li>
2328
{!loading &&
2429
(user ? (
2530
<>
2631
<li>
2732
<Link href="/profile">
28-
<a>Client-rendered profile</a>
33+
<a>Client rendered profile</a>
2934
</Link>
3035
</li>
3136
<li>
@@ -34,12 +39,12 @@ const Header = ({ user, loading }: HeaderProps) => {
3439
</Link>
3540
</li>
3641
<li>
37-
<a href="/api/logout">Logout</a>
42+
<a href="/api/auth/logout">Logout</a>
3843
</li>
3944
</>
4045
) : (
4146
<li>
42-
<a href="/api/login">Login</a>
47+
<a href="/api/auth/login">Login</a>
4348
</li>
4449
))}
4550
</ul>
@@ -63,8 +68,9 @@ const Header = ({ user, loading }: HeaderProps) => {
6368
}
6469
li {
6570
margin-right: 1rem;
71+
padding-right: 2rem;
6672
}
67-
li:nth-child(2) {
73+
li:nth-child(3) {
6874
margin-right: auto;
6975
}
7076
a {

examples/auth0/lib/auth0.ts

-27
This file was deleted.

examples/auth0/lib/user.ts

-76
This file was deleted.

examples/auth0/pages/_app.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { UserProvider } from '@auth0/nextjs-auth0'
2+
3+
export default function App({ Component, pageProps }) {
4+
// optionally pass the 'user' prop from pages that require server-side
5+
// rendering to prepopulate the 'useUser' hook.
6+
7+
const { user } = pageProps
8+
9+
return (
10+
<UserProvider user={user}>
11+
<Component {...pageProps} />
12+
</UserProvider>
13+
)
14+
}

examples/auth0/pages/about.tsx

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
import { useUser } from '@auth0/nextjs-auth0'
12
import Layout from '../components/layout'
2-
import { useFetchUser } from '../lib/user'
33

44
const About = () => {
5-
const { user, loading } = useFetchUser()
5+
const { user, isLoading } = useUser()
66

77
return (
8-
<Layout user={user} loading={loading}>
8+
<Layout user={user} loading={isLoading}>
99
<h1>About</h1>
1010
<p>
11-
This is the about page, navigating between this page and <i>Home</i> is
12-
always pretty fast. However, when you navigate to the <i>Profile</i>{' '}
13-
page it takes more time because it uses SSR to fetch the user first;
11+
This project shows different ways to display Profile info: using{' '}
12+
<i>Client rendered</i>, <i>Server rendered</i>, and <i>API rendered</i>
13+
</p>
14+
<p>
15+
Navigating between this page and <i>Home</i> is always pretty fast.
16+
However, when you navigate to the <i>Server rendered profile</i> page it
17+
takes more time because it uses SSR to fetch the user and then to
18+
display it
1419
</p>
1520
</Layout>
1621
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useEffect, useState } from 'react'
2+
import { useUser } from '@auth0/nextjs-auth0'
3+
import Layout from '../../components/layout'
4+
5+
const ApiProfile = () => {
6+
const { user, isLoading } = useUser()
7+
8+
const [data, setData] = useState(null)
9+
10+
useEffect(() => {
11+
;(async () => {
12+
const res = await fetch('/api/protected-api')
13+
14+
const data = await res.json()
15+
16+
setData(data)
17+
})()
18+
}, [])
19+
20+
return (
21+
<Layout user={user} loading={isLoading}>
22+
<h1>Profile</h1>
23+
24+
<div>
25+
<h3>Public page (client rendered)</h3>
26+
<p>We are fetching data on the client-side :</p>
27+
<p>By making request to '/api/protected-api' serverless function</p>
28+
<p>so without a valid session cookie will fail</p>
29+
<p>{JSON.stringify(data)}</p>
30+
</div>
31+
</Layout>
32+
)
33+
}
34+
35+
// Public route.(CSR) also accessing API from the client-side.
36+
// data is not cached when redirecting between pages.
37+
export default ApiProfile
+5-19
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { GetServerSideProps } from 'next'
2-
3-
// This import is only included in the server build, because it's only used by getServerSideProps
4-
import auth0 from '../../lib/auth0'
1+
import { withPageAuthRequired } from '@auth0/nextjs-auth0'
52
import Layout from '../../components/layout'
63
import { User } from '../../interfaces'
74

85
type ProfileProps = {
96
user: User
107
}
118

12-
const Profile = ({ user }: ProfileProps) => {
9+
export default function Profile({ user }: ProfileProps) {
1310
return (
1411
<Layout user={user}>
1512
<h1>Profile</h1>
@@ -24,17 +21,6 @@ const Profile = ({ user }: ProfileProps) => {
2421
)
2522
}
2623

27-
export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
28-
// Here you can check authentication status directly before rendering the page,
29-
// however the page would be a serverless function, which is more expensive and
30-
// slower than a static page with client side authentication
31-
const session = await auth0.getSession(req, res)
32-
33-
if (!session || !session.user) {
34-
return { redirect: { destination: '/api/login', permanent: false } }
35-
}
36-
37-
return { props: { user: session.user } }
38-
}
39-
40-
export default Profile
24+
// Protected route, checking authentication status before rendering the page.(SSR)
25+
// It's slower than a static page with client side authentication
26+
export const getServerSideProps = withPageAuthRequired()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { handleAuth } from '@auth0/nextjs-auth0'
2+
3+
export default handleAuth()

examples/auth0/pages/api/callback.ts

-15
This file was deleted.

examples/auth0/pages/api/login.ts

-15
This file was deleted.

examples/auth0/pages/api/logout.ts

-15
This file was deleted.

examples/auth0/pages/api/me.ts

-15
This file was deleted.

0 commit comments

Comments
 (0)