Protect your pages with Next.js Middleware. For example:
To protect all your routes:
// middleware.jsimport { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/edge';export default withMiddlewareAuthRequired(); Copy
// middleware.jsimport { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/edge';export default withMiddlewareAuthRequired();
To protect specific routes:
// middleware.jsimport { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/edge';export default withMiddlewareAuthRequired();export const config = { matcher: '/about/:path*',}; Copy
// middleware.jsimport { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/edge';export default withMiddlewareAuthRequired();export const config = { matcher: '/about/:path*',};
For more info see: https://nextjs.org/docs/advanced-features/middleware#matching-paths
To run custom middleware for authenticated users:
// middleware.jsimport { withMiddlewareAuthRequired, getSession } from '@auth0/nextjs-auth0/edge';export default withMiddlewareAuthRequired(async function middleware(req) { const res = NextResponse.next(); const user = await getSession(req, res); res.cookies.set('hl', user.language); return res;}); Copy
// middleware.jsimport { withMiddlewareAuthRequired, getSession } from '@auth0/nextjs-auth0/edge';export default withMiddlewareAuthRequired(async function middleware(req) { const res = NextResponse.next(); const user = await getSession(req, res); res.cookies.set('hl', user.language); return res;});
To provide a custom returnTo url to login:
returnTo
// middleware.jsimport { withMiddlewareAuthRequired, getSession } from '@auth0/nextjs-auth0/edge';export default withMiddlewareAuthRequired({ returnTo: '/foo', // Custom middleware is provided with the `middleware` config option async middleware(req) { return NextResponse.next(); }}); Copy
// middleware.jsimport { withMiddlewareAuthRequired, getSession } from '@auth0/nextjs-auth0/edge';export default withMiddlewareAuthRequired({ returnTo: '/foo', // Custom middleware is provided with the `middleware` config option async middleware(req) { return NextResponse.next(); }});
You can also provide a method for returnTo that takes the req as an argument.
// middleware.jsimport { withMiddlewareAuthRequired, getSession } from '@auth0/nextjs-auth0/edge';export default withMiddlewareAuthRequired({ returnTo(req) { return `${req.nextURL.basePath}${req.nextURL.pathname}`};}); Copy
// middleware.jsimport { withMiddlewareAuthRequired, getSession } from '@auth0/nextjs-auth0/edge';export default withMiddlewareAuthRequired({ returnTo(req) { return `${req.nextURL.basePath}${req.nextURL.pathname}`};});
Optional
Protect your pages with Next.js Middleware. For example:
To protect all your routes:
To protect specific routes:
For more info see: https://nextjs.org/docs/advanced-features/middleware#matching-paths
To run custom middleware for authenticated users:
To provide a custom
returnTo
url to login:You can also provide a method for
returnTo
that takes the req as an argument.