Skip to content

Commit 5a7cde6

Browse files
authored
Merge pull request #186 from auth0-samples/chore/bump-auth0
Migrate nextjs-auth0 to v4
2 parents 77eb33d + 88dc64e commit 5a7cde6

File tree

22 files changed

+212
-182
lines changed

22 files changed

+212
-182
lines changed

Sample-01/.env.local.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
AUTH0_SECRET=replace-with-your-own-secret-generated-with-openssl
2-
AUTH0_BASE_URL=http://localhost:3000
3-
AUTH0_ISSUER_BASE_URL='https://{DOMAIN}'
2+
APP_BASE_URL=http://localhost:3000
3+
AUTH0_DOMAIN='{DOMAIN}'
44
AUTH0_CLIENT_ID='{CLIENT_ID}'
55
AUTH0_CLIENT_SECRET='{CLIENT_SECRET}'
66
AUTH0_AUDIENCE=

Sample-01/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ To do this, first copy `.env.local.example` into a new file in the same folder c
3636
# A long secret value used to encrypt the session cookie
3737
AUTH0_SECRET='LONG_RANDOM_VALUE'
3838
# The base url of your application
39-
AUTH0_BASE_URL='http://localhost:3000'
40-
# The url of your Auth0 tenant domain
41-
AUTH0_ISSUER_BASE_URL='https://YOUR_AUTH0_DOMAIN.auth0.com'
39+
APP_BASE_URL='http://localhost:3000'
40+
# Your Auth0 tenant domain
41+
AUTH0_DOMAIN='YOUR_AUTH0_DOMAIN.auth0.com'
4242
# Your Auth0 application's Client ID
4343
AUTH0_CLIENT_ID='YOUR_AUTH0_CLIENT_ID'
4444
# Your Auth0 application's Client Secret

Sample-01/api-server.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ const jwksRsa = require('jwks-rsa');
99

1010
const app = express();
1111
const port = process.env.API_PORT || 3001;
12-
const baseUrl = process.env.AUTH0_BASE_URL;
13-
const issuerBaseUrl = process.env.AUTH0_ISSUER_BASE_URL;
12+
const baseUrl = process.env.APP_BASE_URL;
13+
const domain = process.env.AUTH0_DOMAIN;
14+
const issuerBaseUrl = `https://${domain}`;
1415
const audience = process.env.AUTH0_AUDIENCE;
1516

16-
if (!baseUrl || !issuerBaseUrl) {
17+
if (!baseUrl || !domain) {
1718
throw new Error('Please make sure that the file .env.local is in place and populated');
1819
}
1920

Sample-01/app/api/auth/[auth0]/route.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

Sample-01/app/api/shows/route.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import { getAccessToken, withApiAuthRequired } from '@auth0/nextjs-auth0';
21
import { NextResponse } from 'next/server';
2+
import { auth0 } from '../../../lib/auth0';
33

4-
export const GET = withApiAuthRequired(async function shows(req) {
4+
export const GET = async function shows() {
55
try {
6+
const session = await auth0.getSession();
7+
8+
if (!session) {
9+
return NextResponse.json(
10+
{ error: 'Not authenticated' },
11+
{ status: 401 }
12+
);
13+
}
14+
615
const res = new NextResponse();
7-
const { accessToken } = await getAccessToken(req, res, {
8-
scopes: ['read:shows']
9-
});
16+
const { token: accessToken } = await auth0.getAccessToken();
1017
const apiPort = process.env.API_PORT || 3001;
1118
const response = await fetch(`http://localhost:${apiPort}/api/shows`, {
1219
headers: {
@@ -19,4 +26,4 @@ export const GET = withApiAuthRequired(async function shows(req) {
1926
} catch (error) {
2027
return NextResponse.json({ error: error.message }, { status: error.status || 500 });
2128
}
22-
});
29+
};

Sample-01/app/csr/page.jsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use client';
22

33
import React from 'react';
4-
import { withPageAuthRequired } from '@auth0/nextjs-auth0/client';
54

6-
export default withPageAuthRequired(function CSRPage() {
5+
export default function CSRPage() {
76
return (
87
<>
98
<div className="mb-5" data-testid="csr">
@@ -20,10 +19,10 @@ export default withPageAuthRequired(function CSRPage() {
2019
custom <a href="https://nextjs.org/docs/advanced-features/custom-app">App Component</a> with it.
2120
</p>
2221
<p>
23-
You can also fetch the user profile by calling the <code>/api/auth/me</code> API route.
22+
You can also fetch the user profile by calling the <code>/auth/profile</code> API route.
2423
</p>
2524
</div>
2625
</div>
2726
</>
2827
);
29-
});
28+
};

Sample-01/app/external/page.jsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import React, { useState } from 'react';
44
import { Button } from 'reactstrap';
5-
import { withPageAuthRequired } from '@auth0/nextjs-auth0/client';
65

76
import Loading from '../../components/Loading';
87
import ErrorMessage from '../../components/ErrorMessage';
98
import Highlight from '../../components/Highlight';
109

11-
function External() {
10+
export default function External() {
1211
const [state, setState] = useState({ isLoading: false, response: undefined, error: undefined });
1312

1413
const callApi = async () => {
@@ -68,8 +67,3 @@ function External() {
6867
</>
6968
);
7069
}
71-
72-
export default withPageAuthRequired(External, {
73-
onRedirecting: () => <Loading />,
74-
onError: error => <ErrorMessage>{error.message}</ErrorMessage>
75-
});

Sample-01/app/layout.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import NavBar from '../components/NavBar';
55
import { Container } from 'reactstrap';
66
import Footer from '../components/Footer';
77
import React from 'react';
8-
import { UserProvider } from '@auth0/nextjs-auth0/client';
8+
import { Auth0Provider } from '@auth0/nextjs-auth0';
99

1010
export default function RootLayout({ children }) {
1111
return (
@@ -20,13 +20,13 @@ export default function RootLayout({ children }) {
2020
<link rel="stylesheet" href="https://cdn.auth0.com/js/auth0-samples-theme/1.0/css/auth0-theme.min.css" />
2121
</head>
2222
<body>
23-
<UserProvider>
23+
<Auth0Provider>
2424
<main id="app" className="d-flex flex-column h-100" data-testid="layout">
2525
<NavBar />
2626
<Container className="flex-grow-1 mt-5">{children}</Container>
2727
<Footer />
2828
</main>
29-
</UserProvider>
29+
</Auth0Provider>
3030
</body>
3131
</html>
3232
);

Sample-01/app/profile/page.jsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import React from 'react';
44
import { Row, Col } from 'reactstrap';
5-
import { useUser, withPageAuthRequired } from '@auth0/nextjs-auth0/client';
5+
import { useUser } from '@auth0/nextjs-auth0';
66

77
import Loading from '../../components/Loading';
8-
import ErrorMessage from '../../components/ErrorMessage';
98
import Highlight from '../../components/Highlight';
109

11-
function Profile() {
10+
export default function Profile() {
1211
const { user, isLoading } = useUser();
1312

1413
return (
@@ -41,8 +40,3 @@ function Profile() {
4140
</>
4241
);
4342
}
44-
45-
export default withPageAuthRequired(Profile, {
46-
onRedirecting: () => <Loading />,
47-
onError: error => <ErrorMessage>{error.message}</ErrorMessage>
48-
});

Sample-01/app/ssr/page.jsx

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
import React from 'react';
2-
import { getSession, withPageAuthRequired } from '@auth0/nextjs-auth0';
2+
import { auth0 } from '../../lib/auth0';
33

44
import Highlight from '../../components/Highlight';
55

6-
export default withPageAuthRequired(
7-
async function SSRPage() {
8-
const { user } = await getSession();
9-
return (
10-
<>
11-
<div className="mb-5" data-testid="ssr">
12-
<h1 data-testid="ssr-title">Server-side Rendered Page</h1>
13-
<div data-testid="ssr-text">
14-
<p>
15-
You can protect a server-side rendered page by wrapping it with <code>withPageAuthRequired</code>. Only
16-
logged in users will be able to access it. If the user is logged out, they will be redirected to the login
17-
page instead.{' '}
18-
</p>
19-
</div>
6+
export default async function SSRPage() {
7+
const { user } = await auth0.getSession();
8+
return (
9+
<>
10+
<div className="mb-5" data-testid="ssr">
11+
<h1 data-testid="ssr-title">Server-side Rendered Page</h1>
12+
<div data-testid="ssr-text">
13+
<p>
14+
You can protect a server-side rendered page by wrapping it with <code>withPageAuthRequired</code>. Only
15+
logged in users will be able to access it. If the user is logged out, they will be redirected to the login
16+
page instead.{' '}
17+
</p>
2018
</div>
21-
<div className="result-block-container" data-testid="ssr-json">
22-
<div className="result-block">
23-
<h6 className="muted">User</h6>
24-
<Highlight>{JSON.stringify(user, null, 2)}</Highlight>
25-
</div>
19+
</div>
20+
<div className="result-block-container" data-testid="ssr-json">
21+
<div className="result-block">
22+
<h6 className="muted">User</h6>
23+
<Highlight>{JSON.stringify(user, null, 2)}</Highlight>
2624
</div>
27-
</>
28-
);
29-
},
30-
{ returnTo: '/ssr' }
31-
);
25+
</div>
26+
</>
27+
);
28+
};

0 commit comments

Comments
 (0)