By NHI Mgmt Group Editorial TeamDomain: Best PracticesSource: DescopePublished July 2, 2025

TL;DR: OIDC-based authentication for a Next.js 13 app can be brokered by NextAuth, including custom provider setup, protected routes, server-side session checks, and an API route guarded by a secret token, according to Descope. The useful lesson is that application authentication is only as strong as the session, callback, and backend access controls wrapped around it.


At a glance

What this is: A hands-on tutorial for adding OIDC-based authentication to a Next.js 13 app with NextAuth, protected routes, and server-side session checks.

Why it matters: It matters because application teams often treat identity as a front-end feature, when the real control plane includes session handling, route protection, and backend authorization boundaries.

👉 Read Descope's tutorial on adding authentication to a Next.js 13 app


Context

Next.js 13 authentication is not just a sign-in button problem. Once an app uses OIDC, the security model depends on how the identity provider, session handling, route guards, and API protections work together across browser and server components.

For IAM practitioners, the important question is whether the application actually enforces authenticated state everywhere it should, especially when server components fetch user data and when a backend route accepts a secret token as part of its trust check. That makes the tutorial relevant to human identity governance, but also to the wider pattern of app-layer access control.

Descope’s walkthrough is a practical example of how modern web apps often inherit identity risk from implementation details rather than from the authentication protocol itself.


Key questions

Q: How should teams secure Next.js 13 routes that depend on user sessions?

A: Use server-side session validation as the enforcement point, and redirect unauthenticated users before protected content is rendered. Client-side hiding is only a presentation control. For sensitive routes, pair session checks with backend authorization so the same identity decision is enforced consistently across pages, API calls, and server components.

Q: Why do backend APIs still need authorization after OIDC login?

A: OIDC proves the user authenticated, but it does not automatically authorise every downstream request. Backend APIs still need their own checks because a valid login session can be misused, replayed, or paired with weak request validation. The safer model is identity at the front door and explicit authorization at every sensitive data boundary.

Q: What do teams get wrong about custom authentication examples?

A: They often copy the sample flow but ignore the trust assumptions hidden in secrets, redirect paths, and callback handling. That works for a demo but becomes fragile in production if the same pattern is used for user data. The key mistake is treating example code as a finished security design.

Q: How can security teams evaluate whether an app auth flow is production-ready?

A: Check whether the app separates authentication from authorization, validates sessions on the server, protects every API route, and has clear ownership for secret handling. If any of those controls are missing, the application may still be easy to use but not yet safe enough for real identities or real data.


Technical breakdown

How NextAuth uses OIDC as the identity broker

NextAuth sits between the application and the identity provider, translating an OAuth or OIDC login into an application session. In this tutorial, Descope is configured as a custom OAuth provider using OIDC discovery, PKCE, and state validation. That combination reduces the amount of custom auth logic the app needs to maintain, while still leaving the application responsible for how the resulting session is used and enforced.

Practical implication: teams should review whether their OIDC setup is only authenticating users or also enforcing the right authorization decisions after login.

Protected routes in Next.js 13 App Router

The App Router changes where authentication checks happen because server components can render before client-side hooks run. In the tutorial, getServerSession is used to determine whether the dashboard should render or redirect to sign-in. That matters because client-side route hiding is not the same as server-side access control. If the session check happens too late, unauthorized users may still reach sensitive data or application logic.

Practical implication: treat server-side session validation as the enforcement point, not the UI as a visibility layer.

Backend API protection with shared secrets and session context

The API route uses a secret token and email parameter to control access to Airtable-backed data. This is a common pattern in early-stage apps, but it introduces a trust split between identity session state and backend request validation. If the secret is weak, reused, or broadly exposed, the API can become a bypass path even when the front end requires login. Identity control is only complete when the backend trusts the authenticated subject, not just the request shape.

Practical implication: review every backend endpoint for its own authorization logic instead of assuming front-end login covers it.


NHI Mgmt Group analysis

Application authentication is not a single control, it is a chain of trust. The tutorial shows how login, session handling, and API access each carry separate enforcement responsibility. That matters because identity teams often think in terms of “the login succeeded,” while attackers think in terms of which downstream control can be bypassed after login. The practitioner takeaway is to govern the whole path, not just the provider handshake.

Server components change where identity decisions must be enforced. In Next.js 13, authentication cannot be treated as a client-only concern because server-rendered pages can fetch data before the browser ever finishes loading. That makes server-side session control part of the identity plane, not a convenience layer. IAM and application security teams should align on where the authoritative access decision is made.

Shared-secret API checks create a brittle trust boundary. A backend route that relies on a secret token plus query parameters can work in a tutorial, but it is easy to overestimate as a durable authorization model. The deeper issue is that the application is mixing identity-based access with static request validation. Practitioners should recognise this as a control boundary that needs lifecycle management and auditable ownership.

OIDC reduces custom authentication code, but it does not remove governance responsibility. Standards-based sign-in simplifies integration, yet the real security work begins after the callback. Session lifetime, redirect logic, protected route behaviour, and backend authorization still define the actual exposure. The implication for identity programmes is clear: protocol compliance is necessary, but operational enforcement is what determines risk.

Developer-authentication patterns now sit inside the identity estate. Hackathon templates, internal tools, and early-stage apps can all become production-adjacent surfaces if they handle real user data. That means IAM, IGA, and security architecture teams need visibility into app-level sign-in implementations, not just enterprise SSO. The practitioner conclusion is that “developer convenience” still needs identity governance.

What this signals

The governance lesson here is that identity controls increasingly live inside application code, not just enterprise IAM platforms. As more teams assemble auth flows from reusable templates, the real risk becomes control drift between the identity provider, the session layer, and the backend endpoint.

That makes app-auth reviews a lifecycle problem as much as an engineering problem. Security teams should expect to see more small authorization shortcuts in developer-facing apps, then decide which ones can be tolerated in non-production use and which must be elevated before real data enters the flow.


For practitioners

  • Review server-side session enforcement Confirm that sensitive Next.js 13 routes use server-side session checks before rendering protected data or redirecting unauthenticated users.
  • Map backend authorization separately Inventory every API route that accepts shared secrets, query parameters, or callback-derived context and define its own authorization rule.
  • Standardise OIDC provider configuration Validate PKCE, state handling, token scope, and redirect behaviour across all custom OIDC integrations rather than copying sample code unchanged.
  • Treat app templates as identity-controlled assets Apply review and ownership checks to hackathon templates, internal portals, and MVPs that may later process real user identities or data.

Key takeaways

  • Next.js 13 authentication only becomes secure when login, session enforcement, and backend authorization are governed as one chain.
  • OIDC simplifies integration, but it does not remove the need to validate access at server-render and API boundaries.
  • Developer templates can become identity-controlled assets once they handle real users, so they need the same review discipline as other access paths.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

NIST SP 800-63, NIST CSF 2.0, NIST Zero Trust (SP 800-207) and NIST SP 800-53 Rev 5 set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
NIST SP 800-63SP 800-63CThe article uses federated login and OIDC-style identity brokering.
NIST CSF 2.0PR.AC-1Protected routes and backend checks are access-control decisions.
NIST Zero Trust (SP 800-207)The tutorial relies on continuous verification before granting access to resources.
NIST SP 800-53 Rev 5AC-2Application sessions and user access need governed account lifecycle handling.

Apply zero trust principles so every route and API validates identity before trust is extended.


Key terms

  • Openid Connect Provider: An OpenID Connect provider is the identity system that authenticates a user and issues identity assertions to another application. In Shopify-style federation, it becomes the external control point for login assurance, token issuance, and session handoff across the customer journey.
  • Server-Side Session: A server-side session is identity state stored or verified on the server rather than trusted entirely in the browser. In React Router apps, this lets loaders and actions decide access before data is returned, which reduces reliance on client-side redirects and makes authorization easier to govern and audit.
  • Protected Route: A protected route is an application path that should only be reachable after identity and access checks succeed. In secure implementations, route protection is enforced server-side and tied to the current session state, not left to client-side visibility or redirects that users can bypass.

What's in the full article

Descope's full tutorial covers the operational detail this post intentionally leaves for the source:

  • Copy-ready code for the custom NextAuth provider configuration and route handler setup
  • Exact component structure for the sign-in button, protected dashboard, and conditional rendering flow
  • Implementation details for the Airtable-backed API route and its request validation logic
  • A working hackathon template layout that shows how the authentication flow fits into a real app

👉 The full Descope guide shows the code structure, dashboard logic, and protected API flow in one build.

Deepen your knowledge

NHI governance, agentic AI identity, and machine identity lifecycle are core topics in our NHI Foundation Level course, the industry's only accredited NHI security programme. If you are building or maturing an IAM programme, it is worth exploring.
NHIMG Editorial Note
Published by the NHIMG editorial team on August 16, 2026.
NHI Mgmt Group — the independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org