Skip to content
VercelLogotypeVercelLogotype
LoginSign Up

Next.js with OpenID Connect

Authenticate using OpenID Connect Protocol via Auth.js

DeployView Demo

Next.js App using Authorization Code Flow with PKCE

This project demonstrates how to implement the Authorization Code Flow with PKCE for a Next.js App.

Disclaimer: This project is for educational purposes only and should not be used in production without proper security review and testing.

Demo

This project is deployed on Cloudflare Pages and on Vercel.

Deploy your own

Deploy the project using Vercel:

Deploy the project using Cloudflare:

Prerequisites

Before getting started, make sure you have the following:

  • Node.js installed on your machine
  • An OpenID Connect provider that supports the Authorization Code Flow with PKCE

Getting Started

  1. Clone the repository:
git clone https://github.com/cerberauth/openid-connect-examples.git
  1. Install the dependencies:
cd openid-connect-examples/nextjs-app
npm ci
  1. Configure the OpenID Connect provider (see the Configuration section below).

  2. Start the development server:

npm run dev
  1. Open your browser and navigate to http://localhost:5173/.

  2. Click on the "Login" button to initiate the authorization code flow.

  3. After successful authentication, you will be redirected back to the application and the user information will be displayed.

Configuration

Configure the OpenID Connect provider:

If you don't have an OpenID Connect provider, you can create a Test Client on the TestID OpenID Connect Provider.

  • Obtain the client ID and client secret from your OpenID Connect provider.
  • Register the redirect URI for your Next.js App in the provider's developer console.

Update the configuration:

  • Create a .env.local file in the root directory of your project or copy .env.example file.
  • Add the necessary environment variables to the .env.local file. For example:
AUTH_SECRET=secret
AUTH_CLIENT_ID=your-client-id
AUTH_CLIENT_SECRET=your-client-secret

Generate a random secret using the following command:

openssl rand -base64 33

Replace secret with the generated secret.

Replace your-client-id and your-client-secret with the actual values provided by your OpenID Connect provider.

Save the .env.local file.

Additional Resources

  • Auth.js
  • OpenID Connect
  • OAuth 2.0 Authorization Code Flow
  • PKCE
  • Awesome OpenID Connect
  • Next.js
  • React
GitHub
Ownercerberauth
Repositoryopenid-connect-examples
Use Cases
Starter
Stack
Next.js
Tailwind
Auth
NextAuth.js

Related Templates

Auth0 React Starter

React application that implements user login, logout and sign-up features using Auth0.
Auth0 React Starter thumbnail

Evervault: Encryption Starter Kit for Next.js

A playground for each of Evervault’s data security and encryption products, built with Next.js and secured with Evervault's SDKs.
Evervault: Encryption Starter Kit for Next.js thumbnail

Agent Stack

  • AI SDK
  • AI Gateway
  • Sandbox
  • Workflows
  • EveNew
  • ConnectNew

Core Platform

  • CI/CD
  • Content Delivery
  • Fluid Compute
  • Observability

Security

  • Platform Security
  • WAF
  • Bot Management
  • Bot ID

Tools

  • Vercel DropNew
  • Vercel Agent
  • Vercel PluginNew
  • Next.js
  • Domains
  • v0

Frameworks

  • Nuxt
  • SvelteKit
  • Nitro
  • Turborepo
  • Tanstack Start
  • FastAPI
  • xmcp
  • All frameworks

SDKs

  • Vercel SDK
  • Workflow SDKNew
  • Flags SDK
  • Chat SDKNew
  • Queues SDKNew
  • Streamdown

Build

  • AI Apps
  • Web Apps
  • Marketing Sites
  • Platforms
  • Commerce
  • Platform Engineers
  • Design Engineers

Learn

  • Docs
  • Blog
  • Changelog
  • Knowledge Base
  • Academy
  • Articles
  • Community

Explore

  • Customers
  • Marketplace
  • Templates
  • Partner Finder
  • Vercel + AWS

Company

  • About
  • Careers
  • Press
  • Events
  • Startups
  • Shipped on Vercel
  • Open Source Program
  • Enterprise
  • Pricing
  • Help

Legal & Trust

  • Privacy Policy
  • Terms of Service
  • Cookie Policy
  • DPA
  • Acceptable Use Policy
  • Legal (all documents)
  • Trust Center
  • Status

Social

  • GitHub
  • X
  • LinkedIn
  • YouTube
  • Instagram
  • VercelVercel
Agent Stack
  • AI SDK
  • AI Gateway
  • Sandbox
  • Workflows
  • Eve
Core Platform
  • Security
  • Content Delivery
  • Fluid Compute
  • Observability
  • CI/CD
Tools
  • Next.js
  • Vercel Agent
  • Vercel Plugin
  • Domains
  • v0
Learn
  • Docs
  • About
  • Blog
  • Changelog
  • Knowledge Base
Build
  • AI Apps
  • Web Apps
  • Marketing Sites
  • Platforms
  • Commerce
Explore
  • Customers
  • Marketplace
  • Partner Finder
  • AWS
  • Community
EnterprisePricing
Contact
Log InSign Up
Dashboard

Products

Agent Stack

  • AI SDK
  • AI Gateway
  • Sandbox
  • Workflows
  • Eve

Core Platform

  • Security
  • Content Delivery
  • Fluid Compute
  • Observability
  • CI/CD

Tools

  • Next.js
  • Vercel Agent
  • Vercel Plugin
  • Domains
  • v0
Resources

Learn

  • Docs
  • About
  • Blog
  • Changelog
  • Knowledge Base

Build

  • AI Apps
  • Web Apps
  • Marketing Sites
  • Platforms
  • Commerce

Explore

  • Customers
  • Marketplace
  • Partner Finder
  • AWS
  • Community
Enterprise
Pricing
Sign UpLog In
Contact
DeployView Demo

Loading status…

Select a display theme: