#how to secure urls
1 messages · Page 1 of 1 (latest)
Hi @deep tulip. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Next.js, securing URLs or routes can be achieved through authentication and authorization. Authentication verifies who a user is, while authorization controls what a user can access. Next.js supports multiple authentication patterns, each designed for different use cases.
For the Pages Router, you can use the following authentication patterns:
- Use static generation (https://nextjs.org/docs/pages/building-your-application/rendering/static-site-generation) to server-render a loading state, followed by fetching user data client-side.
- Fetch user data server-side (https://nextjs.org/docs/pages/building-your-application/rendering/server-side-rendering) to eliminate a flash of unauthenticated content.
For the App Router, there are several common authentication solutions that support it:
- NextAuth.js (https://next-auth.js.org/configuration/nextjs#in-app-router)
- Clerk (https://clerk.com/docs/quickstarts/nextjs)
- Auth0 (https://github.com/auth0/nextjs-auth0#app-router)
- Stytch (https://stytch.com/docs/example-apps/frontend/nextjs)
- Kinde (https://kinde.com/docs/developer-tools/nextjs-sdk/)
- Or manually handling sessions or JWTs
Remember, the App Router takes priority over the Pages Router. Routes across directories should not resolve to the same URL path and will cause a build-time error to prevent a conflict.