@@ -95,9 +95,9 @@ By default, the SDK uses OpenID Connect's RP-Initiated Logout when available, fa
9595
9696``` ts
9797export const auth0 = new Auth0Client ({
98- logoutStrategy: " auto" , // default behavior
98+ logoutStrategy: " auto" // default behavior
9999 // ... other config
100- })
100+ });
101101```
102102
103103Available strategies:
@@ -118,9 +118,9 @@ The `"v2"` strategy is useful for applications that:
118118``` ts
119119// Example: Using v2 logout for wildcard URL support
120120export const auth0 = new Auth0Client ({
121- logoutStrategy: " v2" ,
121+ logoutStrategy: " v2"
122122 // ... other config
123- })
123+ });
124124
125125// This allows logout URLs like:
126126// /auth/logout?returnTo=https://localhost:3000/en/dashboard
@@ -300,6 +300,79 @@ export default withPageAuthRequired(function Page({ user }) {
300300});
301301```
302302
303+ ## Protect an API Route
304+
305+ ### Page Router
306+
307+ Requests to ` /api/protected ` without a valid session cookie will fail with ` 401 ` .
308+
309+ ``` js
310+ // pages/api/protected.js
311+ import { auth0 } from " @/lib/auth0" ;
312+
313+ export default auth0 .withApiAuthRequired (async function myApiRoute (req , res ) {
314+ const { user } = await auth0 .getSession (req);
315+ res .json ({ protected: " My Secret" , id: user .sub });
316+ });
317+ ```
318+
319+ Then you can access your API from the frontend with a valid session cookie.
320+
321+ ``` jsx
322+ // pages/products
323+ import { withPageAuthRequired } from " @auth0/nextjs-auth0/client" ;
324+ import useSWR from " swr" ;
325+
326+ const fetcher = async (uri ) => {
327+ const response = await fetch (uri);
328+ return response .json ();
329+ };
330+
331+ export default withPageAuthRequired (function Products () {
332+ const { data , error } = useSWR (" /api/protected" , fetcher);
333+ if (error) return < div> oops... {error .message }< / div> ;
334+ if (data === undefined ) return < div> Loading... < / div> ;
335+ return < div> {data .protected }< / div> ;
336+ });
337+ ```
338+
339+ ### App Router
340+
341+ Requests to ` /api/protected ` without a valid session cookie will fail with ` 401 ` .
342+
343+ ``` js
344+ // app/api/protected/route.js
345+ import { auth0 } from " @/lib/auth0" ;
346+
347+ export const GET = auth0 .withApiAuthRequired (async function myApiRoute (req ) {
348+ const res = new NextResponse ();
349+ const { user } = await auth0 .getSession (req);
350+ return NextResponse .json ({ protected: " My Secret" , id: user .sub }, res);
351+ });
352+ ```
353+
354+ Then you can access your API from the frontend with a valid session cookie.
355+
356+ ``` jsx
357+ // app/products/page.jsx
358+ " use client" ;
359+
360+ import { withPageAuthRequired } from " @auth0/nextjs-auth0/client" ;
361+ import useSWR from " swr" ;
362+
363+ const fetcher = async (uri ) => {
364+ const response = await fetch (uri);
365+ return response .json ();
366+ };
367+
368+ export default withPageAuthRequired (function Products () {
369+ const { data , error } = useSWR (" /api/protected" , fetcher);
370+ if (error) return < div> oops... {error .message }< / div> ;
371+ if (data === undefined ) return < div> Loading... < / div> ;
372+ return < div> {data .protected }< / div> ;
373+ });
374+ ```
375+
303376## Accessing the idToken
304377
305378` idToken ` can be accessed from the session in the following way:
0 commit comments