This repository provides an example implementation of OIDC authentication for Next.js App Router applications, focusing on server-side session management, token refreshing, and seamless client-server state synchronization.
This example is discussed in detail in the blog post: Implementing OIDC Authentication in Next.js App Router — Please refer to the post for an in-depth explanation of the concepts, challenges, and code examples.
To use this example, set up an OIDC client with Google:
- Go to the Google Cloud Console.
- Create a new project (or select an existing one).
- Navigate to APIs & Services > OAuth consent screen and configure your consent screen.
- Go to Credentials > Create Credentials > OAuth 2.0 Client IDs.
- Choose Web application as the application type.
- Under Authorized redirect URIs, add: http://localhost:3000/api/auth/callback
- Save the credentials and note down the Client ID and Client Secret.
Create a .env.local file in the root of the repository and add the following variables:
OIDC_LOGIN_URL=https://accounts.google.com
OIDC_CLIENT_ID=your-google-client-id
OIDC_CLIENT_SECRET=your-google-client-secret
NEXT_PUBLIC_FULL_APP_URL=http://localhost:3000
Replace your-google-client-id and your-google-client-secret with the credentials obtained from the Google Cloud Console.
run the development server:
npm run dev
Open http://localhost:3000 with your browser to see the result.