NestJS is a progressive Node.js framework for building efficient, reliable, and scalable server-side applications using TypeScript. This example demonstrates how to integrate Zitadel using the OAuth 2.0 PKCE flow to authenticate users securely and maintain sessions across your NestJS application.
This example implements a complete authentication flow using PKCE with Zitadel with Zitadel as the identity provider. Users begin on a public landing page and click a login button to authenticate through Zitadel's authorization server. After successful authentication, they're redirected to a protected profile page displaying their user information retrieved from the ID token with Zitadel as the identity provider. Users begin on a public landing page and click a login button to authenticate through Zitadel's authorization server. After successful authentication, they're redirected to a protected profile page displaying their user information retrieved from the ID token and access token.
The logout implementation demonstrates federated logout by redirecting users to Zitadel's end-session endpoint, terminating both the local application session and the Zitadel session. CSRF protection by redirecting users to Zitadel's end-session endpoint, terminating both the local application session and the Zitadel session. CSRF protection during logout is achieved through a state parameter validated in the callback. The example also showcases accessing Zitadel's UserInfo endpoint to fetch real-time user data, including custom claims with Zitadel as the identity provider. Users begin on a public landing page and click a login button to authenticate through Zitadel's authorization server. After successful authentication, they're redirected to a protected profile page displaying their user information retrieved from the ID token, roles by redirecting users to Zitadel's end-session endpoint, terminating both the local application session and the Zitadel session. CSRF protection during logout is achieved through a state parameter validated in the callback. The example also showcases accessing Zitadel's UserInfo endpoint to fetch real-time user data, including custom claims, roles, and organization membership.
All protected routes are secured using the @mridang/nestjs-auth global guard, with the @Public() decorator marking routes that don't require authentication. The application uses Handlebars templates for server-side rendering and Tailwind CSS for styling, providing a complete reference implementation for NestJS developers.
Before running this example, you need to create and configure a PKCE application in the ZITADEL Management Console. Follow the PKCE application setup guide to:
Note: Make sure to enable Dev Mode in the ZITADEL Management Console if you're using HTTP URLs during local development. For production, always use HTTPS URLs and disable Dev Mode.
Create a .env file in the project root and configure it with the values from your Zitadel application. Use the exact environment variable names from the repository: