Skip to Content
Introduction

@authgear/nextjs

Authgear SDK for Next.js 16 — integrate authentication into your Next.js application with a few lines of code.

What it does

  • OAuth / OIDC — PKCE-based sign-in and sign-out with any Authgear-hosted identity provider
  • Session management — encrypted, httpOnly session cookie (AES-256-GCM); automatic token refresh
  • Server utilities — read the current user in Server Components and Route Handlers
  • JWT verification — protect API routes by verifying Bearer tokens via JWKS
  • Route protection — Next.js 16 proxy.ts that redirects unauthenticated requests and injects Authorization headers

Entry points

ImportWhat it provides
@authgear/nextjscreateAuthgearHandlers() — OAuth route handler
@authgear/nextjs/serverauth(), currentUser(), verifyAccessToken() — server-only
@authgear/nextjs/clientAuthgearProvider, useAuthgear(), SignInButton, SignOutButton
@authgear/nextjs/proxycreateAuthgearProxy() — Next.js 16 proxy helper

Quick example

// lib/authgear.ts export const authgearConfig: AuthgearConfig = { endpoint: process.env.AUTHGEAR_ENDPOINT!, clientID: process.env.AUTHGEAR_CLIENT_ID!, redirectURI: process.env.AUTHGEAR_REDIRECT_URI!, sessionSecret: process.env.SESSION_SECRET!, };
// app/api/auth/[...authgear]/route.ts import { createAuthgearHandlers } from "@authgear/nextjs"; export const { GET, POST } = createAuthgearHandlers(authgearConfig);
// app/dashboard/page.tsx (Server Component) import { currentUser } from "@authgear/nextjs/server"; const user = await currentUser(authgearConfig);
Last updated on