Skip to content

Commit 854d6c8

Browse files
committed
docs: updated the docs as it referenced the incorrect framework
1 parent 602b73e commit 854d6c8

File tree

1 file changed

+72
-44
lines changed

1 file changed

+72
-44
lines changed

README.md

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
# Hono with ZITADEL
1+
# NestJS with ZITADEL
22

3-
[Hono](https://hono.dev/) is a small, simple, and ultrafast web framework for the Edges. It works on Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Netlify, AWS Lambda, Lambda@Edge, and Node.js. In a modern setup, your Hono application manages both your application's frontend and backend logic through server-side routes and middleware.
3+
[NestJS](https://nestjs.com/) is a progressive Node.js framework for building efficient, reliable, and scalable server-side applications. It uses modern JavaScript, is built with TypeScript, and combines elements of Object Oriented Programming, Functional Programming, and Functional Reactive Programming. NestJS provides a robust architecture with modules, controllers, and dependency injection out of the box.
44

5-
To secure such an application, you need a reliable way to handle user logins. For the Hono ecosystem, [Auth.js](https://authjs.dev/) (formerly NextAuth.js) is the standard and recommended library for authentication. Think of it as a flexible security guard for your app. This guide demonstrates how to use Auth.js with a Hono application to implement a secure login with ZITADEL.
5+
To secure such an application, you need a reliable way to handle user logins. For the NestJS ecosystem, this example uses [@mridang/nestjs-auth](https://github.com/mridang/nestjs-auth), a NestJS wrapper around [Auth.js](https://authjs.dev/) (formerly NextAuth.js). Think of it as a flexible security guard for your app. This guide demonstrates how to use Auth.js with a NestJS application to implement a secure login with ZITADEL.
66

77
We'll be using the **OpenID Connect (OIDC)** protocol with the **Authorization Code Flow + PKCE**. This is the industry-best practice for security, ensuring that the login process is safe from start to finish. You can learn more in our [guide to OAuth 2.0 recommended flows](https://zitadel.com/docs/guides/integrate/login/oidc/oauth-recommended-flows).
88

9-
This example uses **Auth.js**, the standard for Hono authentication. While ZITADEL doesn't offer a specific SDK, Auth.js is highly modular. It works with a "provider" that handles the communication with ZITADEL. Under the hood, this example uses the powerful OIDC standard to manage the secure PKCE flow.
9+
This example uses **@mridang/nestjs-auth**, which integrates Auth.js seamlessly into NestJS applications. While ZITADEL doesn't offer a specific SDK, Auth.js is highly modular. It works with a "provider" that handles the communication with ZITADEL. Under the hood, this example uses the powerful OIDC standard to manage the secure PKCE flow.
1010

1111
Check out our Example Application to see it in action.
1212

1313
## Example Application
1414

15-
The example repository includes a complete Hono application, ready to run, that demonstrates how to integrate ZITADEL for user authentication.
15+
The example repository includes a complete NestJS application, ready to run, that demonstrates how to integrate ZITADEL for user authentication.
1616

17-
This example application showcases a typical web app authentication pattern: users start on a public landing page, click a login button to authenticate with ZITADEL, and are then redirected to a protected profile page displaying their user information. The app also includes secure logout functionality that clears the session and redirects users back to ZITADEL's logout endpoint. All protected routes are automatically secured using Auth.js middleware and session management, ensuring only authenticated users can access sensitive areas of your application.
17+
This example application showcases a typical web app authentication pattern: users start on a public landing page, click a login button to authenticate with ZITADEL, and are then redirected to a protected profile page displaying their user information. The app also includes secure logout functionality that clears the session and redirects users back to ZITADEL's logout endpoint. All protected routes are automatically secured using NestJS Guards and session management, ensuring only authenticated users can access sensitive areas of your application.
1818

1919
### Prerequisites
2020

@@ -34,26 +34,40 @@ You'll need a ZITADEL account and application configured. Follow the [ZITADEL do
3434
> - **Redirect URIs:** Add `http://localhost:3000/auth/callback` (for development)
3535
> - **Post Logout Redirect URIs:** Add `http://localhost:3000/auth/logout/callback` (for development)
3636
>
37-
> These URLs must exactly match what your Hono application uses. For production, add your production URLs.
37+
> These URLs must exactly match what your NestJS application uses. For production, add your production URLs.
3838
3939
### Configuration
4040

4141
To run the application, you first need to copy the `.env.example` file to a new file named `.env.local` and fill in your ZITADEL application credentials.
4242

4343
```dotenv
44-
# Port number where your Hono server will listen for incoming HTTP requests.
45-
# Change this if port 3000 is already in use on your system.
46-
PORT=3000
44+
# The environment in which the application is running. This should be set to
45+
# 'production' on your live server to enable security features like secure
46+
# cookies. For local development, 'development' is appropriate.
47+
NODE_ENV=development
4748
48-
# Session timeout in seconds. Users will be automatically logged out after this
49-
# duration of inactivity. 3600 seconds = 1 hour.
50-
SESSION_DURATION=3600
49+
# The network port on which the NestJS server will listen for incoming
50+
# connections. Change this if port 3000 is already in use on your system.
51+
PORT=3000
5152
52-
# Secret key used to cryptographically sign session cookies to prevent
53-
# tampering. MUST be a long, random string. Generate a secure key using:
53+
# A long, random, and secret string used to sign the session cookie. This
54+
# prevents the cookie from being tampered with. It must be kept private.
55+
# Generate a secure key using:
5456
# node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
5557
SESSION_SECRET="your-very-secret-and-strong-session-key"
5658
59+
# A cryptographic salt used in combination with the session secret to
60+
# derive the encryption keys for the session cookie. This should be a
61+
# random string of at least 16 characters.
62+
# Generate a secure salt using:
63+
# node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
64+
SESSION_SALT="your-very-secret-and-strong-session-salt"
65+
66+
# The total duration of the session in milliseconds. After this period of
67+
# inactivity, the user will be effectively logged out.
68+
# Default is 3600000, which is 1 hour (60 * 60 * 1000).
69+
SESSION_DURATION=3600000
70+
5771
# Your ZITADEL instance domain URL. Found in your ZITADEL console under
5872
# instance settings. Include the full https:// URL.
5973
# Example: https://my-company-abc123.zitadel.cloud
@@ -64,25 +78,23 @@ ZITADEL_DOMAIN="https://your-zitadel-domain"
6478
# request.
6579
ZITADEL_CLIENT_ID="your-client-id"
6680
67-
# While the Authorization Code Flow with PKCE for public clients
68-
# does not strictly require a client secret for OIDC specification compliance,
69-
# AuthJS will still require a value for its internal configuration.
70-
# Therefore, please provide a randomly generated string here.
71-
# You can generate a secure key using:
72-
# node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
73-
ZITADEL_CLIENT_SECRET="your-randomly-generated-client-secret"
81+
# The Client Secret for your application. This is only required if you have
82+
# configured your ZITADEL application as "Confidential". For public clients,
83+
# like single-page apps, this can be left empty.
84+
ZITADEL_CLIENT_SECRET=""
7485
7586
# OAuth callback URL where ZITADEL redirects after user authentication. This
7687
# MUST exactly match a Redirect URI configured in your ZITADEL application.
7788
ZITADEL_CALLBACK_URL="http://localhost:3000/auth/callback"
7889
90+
# The internal URL within your application where users are sent after a
91+
# successful login is processed at the callback URL.
92+
# Defaults to "/profile" if not specified.
93+
ZITADEL_POST_LOGIN_URL="/profile"
94+
7995
# URL where users are redirected after logout. This should match a Post Logout
8096
# Redirect URI configured in your ZITADEL application settings.
81-
ZITADEL_POST_LOGOUT_URL="http://localhost:3000/auth/logout/callback"
82-
83-
# Auth.js base URL for your application. In development, this is typically
84-
# http://localhost:3000. In production, use your actual domain.
85-
NEXTAUTH_URL="http://localhost:3000"
97+
ZITADEL_POST_LOGOUT_URL="http://localhost:3000"
8698
```
8799

88100
### Installation and Running
@@ -91,9 +103,9 @@ Follow these steps to get the application running:
91103

92104
```bash
93105
# 1. Clone the repository
94-
git clone [email protected]:zitadel/example-auth-hono.git
106+
git clone [email protected]:zitadel/example-auth-nestjs.git
95107

96-
cd example-auth-hono
108+
cd example-auth-nestjs
97109

98110
# 2. Install the project dependencies
99111
npm install
@@ -116,30 +128,45 @@ Built-in session management with Auth.js handles user authentication state acros
116128

117129
### Route Protection
118130

119-
Protected routes automatically redirect unauthenticated users to the login flow, ensuring sensitive areas of your application remain secure.
131+
Protected routes automatically redirect unauthenticated users to the login flow using NestJS Guards, ensuring sensitive areas of your application remain secure. The `@Public()` decorator allows specific routes to bypass authentication.
132+
133+
### NestJS Integration
134+
135+
Leverages NestJS features including:
136+
- **Guards**: Global authentication guard with `@Public()` decorator for public routes
137+
- **Decorators**: `@AuthSession()` parameter decorator for accessing session data
138+
- **Modules**: Clean separation of concerns with dedicated AuthModule
139+
- **Dependency Injection**: Seamless integration with NestJS's DI system
120140

121141
### Logout Flow
122142

123143
Complete logout implementation that properly terminates both the local session and the ZITADEL session, with proper redirect handling.
124144

125145
## TODOs
126146

127-
### 1. Security headers (Hono built-in)
147+
### 1. Security headers (NestJS middleware)
148+
149+
**Not enabled.** Consider adding security headers using NestJS middleware or the helmet package:
150+
151+
```typescript
152+
import helmet from 'helmet';
128153

129-
**Partially enabled.** Hono includes some security headers by default, but consider adding custom headers in your application:
154+
async function bootstrap() {
155+
const app = await NestFactory.create(AppModule);
130156

131-
```javascript
132-
import { secureHeaders } from 'hono/secure-headers';
157+
app.use(
158+
helmet({
159+
contentSecurityPolicy: {
160+
directives: {
161+
defaultSrc: ["'self'"],
162+
scriptSrc: ["'self'", "'unsafe-eval'", "'unsafe-inline'"],
163+
},
164+
},
165+
}),
166+
);
133167

134-
app.use(
135-
'*',
136-
secureHeaders({
137-
contentSecurityPolicy: {
138-
defaultSrc: ["'self'"],
139-
scriptSrc: ["'self'", "'unsafe-eval'", "'unsafe-inline'"],
140-
},
141-
}),
142-
);
168+
await app.listen(3000);
169+
}
143170
```
144171

145172
At minimum, configure:
@@ -151,6 +178,7 @@ At minimum, configure:
151178

152179
## Resources
153180

154-
- **Hono Documentation:** <https://hono.dev/>
181+
- **NestJS Documentation:** <https://nestjs.com/>
182+
- **@mridang/nestjs-auth:** <https://github.com/mridang/nestjs-auth>
155183
- **Auth.js Documentation:** <https://authjs.dev/>
156184
- **ZITADEL Documentation:** <https://zitadel.com/docs>

0 commit comments

Comments
 (0)