Skip to content

Commit 210dd5f

Browse files
committed
chore: add examples
1 parent b8fe9ae commit 210dd5f

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

EXAMPLES.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
- [On the server (App Router)](#on-the-server-app-router)
1010
- [On the server (Pages Router)](#on-the-server-pages-router)
1111
- [Middleware](#middleware)
12+
- [Protecting a Server-Side Rendered (SSR) Page](#protecting-a-server-side-rendered-ssr-page)
13+
- [Page Router](#page-router)
14+
- [App Router](#app-router)
1215
- [Protecting a Client-Side Rendered (CSR) Page](#protecting-a-client-side-rendered-csr-page)
1316
- [Accessing the idToken](#accessing-the-idtoken)
1417
- [Updating the session](#updating-the-session)
@@ -201,6 +204,43 @@ export async function middleware(request: NextRequest) {
201204
> [!IMPORTANT]
202205
> The `request` object must be passed as a parameter to the `getSession(request)` method when called from a middleware to ensure that any updates to the session can be read within the same request.
203206
207+
## Protecting a Server-Side Rendered (SSR) Page
208+
209+
#### Page Router
210+
211+
Requests to `/pages/profile` without a valid session cookie will be redirected to the login page.
212+
213+
```jsx
214+
// pages/profile.js
215+
import { auth0 } from "@/lib/auth0";
216+
217+
export default function Profile({ user }) {
218+
return <div>Hello {user.name}</div>;
219+
}
220+
221+
// You can optionally pass your own `getServerSideProps` function into
222+
// `withPageAuthRequired` and the props will be merged with the `user` prop
223+
export const getServerSideProps = auth0.withPageAuthRequired();
224+
```
225+
226+
#### App Router
227+
228+
Requests to `/profile` without a valid session cookie will be redirected to the login page.
229+
230+
```jsx
231+
// app/profile/page.js
232+
import { auth0 } from "@/lib/auth0";
233+
234+
export default auth0.withPageAuthRequired(
235+
async function Profile() {
236+
const { user } = await auth0.getSession();
237+
return <div>Hello {user.name}</div>;
238+
},
239+
{ returnTo: "/profile" }
240+
);
241+
// You need to provide a `returnTo` since Server Components aren't aware of the page's URL
242+
```
243+
204244
## Protecting a Client-Side Rendered (CSR) Page
205245

206246
To protect a Client-Side Rendered (CSR) page, you can use the `withPageAuthRequired` higher-order function. Requests to `/profile` without a valid session cookie will be redirected to the login page.

src/server/helpers/with-page-auth-required.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { redirect } from "next/navigation.js";
55
import ReactDOMServer from "react-dom/server";
66
import { afterEach, describe, expect, it, vi } from "vitest";
77

8-
import { getDefaultRoutes } from "../../test/defaults.js";
98
import { generateSecret } from "../../test/utils.js";
109
import { Auth0Client } from "../client.js";
1110
import { RequestCookies } from "../cookies.js";

src/server/helpers/with-page-auth-required.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
GetServerSidePropsContext,
66
GetServerSidePropsResult
77
} from "next";
8-
import { redirect } from "next/navigation.js";
98

109
import { User } from "../../types/index.js";
1110
import { Auth0Client } from "../client.js";
@@ -195,6 +194,7 @@ export const appRouteHandlerFactory =
195194
typeof opts.returnTo === "function"
196195
? await opts.returnTo(params)
197196
: opts.returnTo;
197+
const { redirect } = await import("next/navigation.js");
198198
redirect(
199199
`${config.loginUrl}${opts.returnTo ? `?returnTo=${returnTo}` : ""}`
200200
);

0 commit comments

Comments
 (0)