You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[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.
4
4
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.
6
6
7
7
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).
8
8
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.
10
10
11
11
Check out our Example Application to see it in action.
12
12
13
13
## Example Application
14
14
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.
16
16
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.
18
18
19
19
### Prerequisites
20
20
@@ -34,26 +34,40 @@ You'll need a ZITADEL account and application configured. Follow the [ZITADEL do
> 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.
38
38
39
39
### Configuration
40
40
41
41
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.
42
42
43
43
```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
47
48
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
51
52
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.
@@ -116,30 +128,45 @@ Built-in session management with Auth.js handles user authentication state acros
116
128
117
129
### Route Protection
118
130
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
120
140
121
141
### Logout Flow
122
142
123
143
Complete logout implementation that properly terminates both the local session and the ZITADEL session, with proper redirect handling.
124
144
125
145
## TODOs
126
146
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
+
importhelmetfrom'helmet';
128
153
129
-
**Partially enabled.** Hono includes some security headers by default, but consider adding custom headers in your application:
0 commit comments