@ranimontagna/agent-toolkit 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +287 -264
  2. package/dist/src/args.js +10 -0
  3. package/dist/src/args.js.map +1 -1
  4. package/dist/src/menu.js +47 -4
  5. package/dist/src/menu.js.map +1 -1
  6. package/dist/src/skills.d.ts +1 -0
  7. package/dist/src/skills.js +28 -6
  8. package/dist/src/skills.js.map +1 -1
  9. package/dist/src/state.d.ts +2 -0
  10. package/dist/src/state.js +7 -0
  11. package/dist/src/state.js.map +1 -1
  12. package/dist/src/status.js +6 -2
  13. package/dist/src/status.js.map +1 -1
  14. package/dist/src/usage.js +2 -0
  15. package/dist/src/usage.js.map +1 -1
  16. package/docs/assets/install-plan.svg +29 -0
  17. package/docs/assets/install-skill-packages.svg +31 -0
  18. package/docs/assets/install-status.svg +32 -0
  19. package/package.json +10 -9
  20. package/setup-agent-toolkit.sh +1 -1
  21. package/skills/backend/fastify-best-practices/LICENSE +21 -0
  22. package/skills/backend/fastify-best-practices/NOTICE.md +11 -0
  23. package/skills/backend/fastify-best-practices/SKILL.md +75 -0
  24. package/skills/backend/fastify-best-practices/rules/authentication.md +521 -0
  25. package/skills/backend/fastify-best-practices/rules/configuration.md +217 -0
  26. package/skills/backend/fastify-best-practices/rules/content-type.md +387 -0
  27. package/skills/backend/fastify-best-practices/rules/cors-security.md +445 -0
  28. package/skills/backend/fastify-best-practices/rules/database.md +320 -0
  29. package/skills/backend/fastify-best-practices/rules/decorators.md +416 -0
  30. package/skills/backend/fastify-best-practices/rules/deployment.md +423 -0
  31. package/skills/backend/fastify-best-practices/rules/error-handling.md +412 -0
  32. package/skills/backend/fastify-best-practices/rules/hooks.md +464 -0
  33. package/skills/backend/fastify-best-practices/rules/http-proxy.md +247 -0
  34. package/skills/backend/fastify-best-practices/rules/logging.md +402 -0
  35. package/skills/backend/fastify-best-practices/rules/performance.md +425 -0
  36. package/skills/backend/fastify-best-practices/rules/plugins.md +320 -0
  37. package/skills/backend/fastify-best-practices/rules/routes.md +467 -0
  38. package/skills/backend/fastify-best-practices/rules/schemas.md +585 -0
  39. package/skills/backend/fastify-best-practices/rules/serialization.md +475 -0
  40. package/skills/backend/fastify-best-practices/rules/testing.md +536 -0
  41. package/skills/backend/fastify-best-practices/rules/typescript.md +458 -0
  42. package/skills/backend/fastify-best-practices/rules/websockets.md +421 -0
  43. package/skills/backend/fastify-best-practices/tile.json +11 -0
  44. package/skills/core/agent-toolkit-maintainer/SKILL.md +16 -14
@@ -13,7 +13,7 @@ CLI_ENTRYPOINT="$SCRIPT_DIR/dist/bin/agent-toolkit.js"
13
13
 
14
14
  if [[ ! -f "$CLI_ENTRYPOINT" ]]; then
15
15
  echo "Agent Toolkit build not found: dist/bin/agent-toolkit.js" >&2
16
- echo "Run npm install && npm run build, then run this command again." >&2
16
+ echo "Run pnpm install && pnpm run build, then run this command again." >&2
17
17
  exit 1
18
18
  fi
19
19
 
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Matteo Collina
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,11 @@
1
+ # Attribution
2
+
3
+ This skill is copied from Matteo Collina's public `mcollina/skills` repository.
4
+
5
+ - Source: https://github.com/mcollina/skills/tree/main/skills/fastify
6
+ - Imported from commit: `5b2a81354b6d10325da0db9decc9ce5ecc714138`
7
+ - Upstream package name: `mcollina/fastify-best-practices`
8
+ - License: MIT
9
+ - Copyright: Copyright (c) 2026 Matteo Collina
10
+
11
+ The upstream MIT license text is preserved in `LICENSE`.
@@ -0,0 +1,75 @@
1
+ ---
2
+ name: fastify-best-practices
3
+ description: "Guides development of Fastify Node.js backend servers and REST APIs using TypeScript or JavaScript. Use when building, configuring, or debugging a Fastify application — including defining routes, implementing plugins, setting up JSON Schema validation, handling errors, optimising performance, managing authentication, configuring CORS and security headers, integrating databases, working with WebSockets, and deploying to production. Covers the full Fastify request lifecycle (hooks, serialization, logging with Pino) and TypeScript integration via strip types. Trigger terms: Fastify, Node.js server, REST API, API routes, backend framework, fastify.config, server.ts, app.ts."
4
+ metadata:
5
+ tags: fastify, nodejs, typescript, backend, api, server, http
6
+ ---
7
+
8
+ ## When to use
9
+
10
+ Use this skill when you need to:
11
+ - Develop backend applications using Fastify
12
+ - Implement Fastify plugins and route handlers
13
+ - Get guidance on Fastify architecture and patterns
14
+ - Use TypeScript with Fastify (strip types)
15
+ - Implement testing with Fastify's inject method
16
+ - Configure validation, serialization, and error handling
17
+
18
+ ## Quick Start
19
+
20
+ A minimal, runnable Fastify server to get started immediately:
21
+
22
+ ```ts
23
+ import Fastify from 'fastify'
24
+
25
+ const app = Fastify({ logger: true })
26
+
27
+ app.get('/health', async (request, reply) => {
28
+ return { status: 'ok' }
29
+ })
30
+
31
+ const start = async () => {
32
+ await app.listen({ port: 3000, host: '0.0.0.0' })
33
+ }
34
+ start()
35
+ ```
36
+
37
+ ## Recommended Reading Order for Common Scenarios
38
+
39
+ - **New to Fastify?** Start with `plugins.md` → `routes.md` → `schemas.md`
40
+ - **Adding authentication:** `plugins.md` → `hooks.md` → `authentication.md`
41
+ - **Improving performance:** `schemas.md` → `serialization.md` → `performance.md`
42
+ - **Setting up testing:** `routes.md` → `testing.md`
43
+ - **Going to production:** `logging.md` → `configuration.md` → `deployment.md`
44
+
45
+ ## How to use
46
+
47
+ Read individual rule files for detailed explanations and code examples:
48
+
49
+ - [rules/plugins.md](rules/plugins.md) - Plugin development and encapsulation
50
+ - [rules/routes.md](rules/routes.md) - Route organization and handlers
51
+ - [rules/schemas.md](rules/schemas.md) - JSON Schema validation
52
+ - [rules/error-handling.md](rules/error-handling.md) - Error handling patterns
53
+ - [rules/hooks.md](rules/hooks.md) - Hooks and request lifecycle
54
+ - [rules/authentication.md](rules/authentication.md) - Authentication and authorization
55
+ - [rules/testing.md](rules/testing.md) - Testing with inject()
56
+ - [rules/performance.md](rules/performance.md) - Performance optimization
57
+ - [rules/logging.md](rules/logging.md) - Logging with Pino
58
+ - [rules/typescript.md](rules/typescript.md) - TypeScript integration
59
+ - [rules/decorators.md](rules/decorators.md) - Decorators and extensions
60
+ - [rules/content-type.md](rules/content-type.md) - Content type parsing
61
+ - [rules/serialization.md](rules/serialization.md) - Response serialization
62
+ - [rules/cors-security.md](rules/cors-security.md) - CORS and security headers
63
+ - [rules/websockets.md](rules/websockets.md) - WebSocket support
64
+ - [rules/database.md](rules/database.md) - Database integration patterns
65
+ - [rules/configuration.md](rules/configuration.md) - Application configuration
66
+ - [rules/deployment.md](rules/deployment.md) - Production deployment
67
+ - [rules/http-proxy.md](rules/http-proxy.md) - HTTP proxying and reply.from()
68
+
69
+ ## Core Principles
70
+
71
+ - **Encapsulation**: Fastify's plugin system provides automatic encapsulation
72
+ - **Schema-first**: Define schemas for validation and serialization
73
+ - **Performance**: Fastify is optimized for speed; use its features correctly
74
+ - **Async/await**: All handlers and hooks support async functions
75
+ - **Minimal dependencies**: Prefer Fastify's built-in features and official plugins
@@ -0,0 +1,521 @@
1
+ ---
2
+ name: authentication
3
+ description: Authentication and authorization patterns in Fastify
4
+ metadata:
5
+ tags: auth, jwt, session, oauth, security, authorization
6
+ ---
7
+
8
+ # Authentication and Authorization
9
+
10
+ ## JWT Authentication with @fastify/jwt
11
+
12
+ Use `@fastify/jwt` for JSON Web Token authentication:
13
+
14
+ ```typescript
15
+ import Fastify from 'fastify';
16
+ import fastifyJwt from '@fastify/jwt';
17
+
18
+ const app = Fastify();
19
+
20
+ app.register(fastifyJwt, {
21
+ secret: process.env.JWT_SECRET,
22
+ sign: {
23
+ expiresIn: '1h',
24
+ },
25
+ });
26
+
27
+ // Decorate request with authentication method
28
+ app.decorate('authenticate', async function (request, reply) {
29
+ try {
30
+ await request.jwtVerify();
31
+ } catch (err) {
32
+ reply.code(401).send({ error: 'Unauthorized' });
33
+ }
34
+ });
35
+
36
+ // Login route
37
+ app.post('/login', {
38
+ schema: {
39
+ body: {
40
+ type: 'object',
41
+ properties: {
42
+ email: { type: 'string', format: 'email' },
43
+ password: { type: 'string' },
44
+ },
45
+ required: ['email', 'password'],
46
+ },
47
+ },
48
+ }, async (request, reply) => {
49
+ const { email, password } = request.body;
50
+ const user = await validateCredentials(email, password);
51
+
52
+ if (!user) {
53
+ return reply.code(401).send({ error: 'Invalid credentials' });
54
+ }
55
+
56
+ const token = app.jwt.sign({
57
+ id: user.id,
58
+ email: user.email,
59
+ role: user.role,
60
+ });
61
+
62
+ return { token };
63
+ });
64
+
65
+ // Protected route
66
+ app.get('/profile', {
67
+ onRequest: [app.authenticate],
68
+ }, async (request) => {
69
+ return { user: request.user };
70
+ });
71
+ ```
72
+
73
+ ## Refresh Tokens
74
+
75
+ Implement refresh token rotation:
76
+
77
+ ```typescript
78
+ import fastifyJwt from '@fastify/jwt';
79
+ import { randomBytes } from 'node:crypto';
80
+
81
+ app.register(fastifyJwt, {
82
+ secret: process.env.JWT_SECRET,
83
+ sign: {
84
+ expiresIn: '15m', // Short-lived access tokens
85
+ },
86
+ });
87
+
88
+ // Store refresh tokens (use Redis in production)
89
+ const refreshTokens = new Map<string, { userId: string; expires: number }>();
90
+
91
+ app.post('/auth/login', async (request, reply) => {
92
+ const { email, password } = request.body;
93
+ const user = await validateCredentials(email, password);
94
+
95
+ if (!user) {
96
+ return reply.code(401).send({ error: 'Invalid credentials' });
97
+ }
98
+
99
+ const accessToken = app.jwt.sign({ id: user.id, role: user.role });
100
+ const refreshToken = randomBytes(32).toString('hex');
101
+
102
+ refreshTokens.set(refreshToken, {
103
+ userId: user.id,
104
+ expires: Date.now() + 7 * 24 * 60 * 60 * 1000, // 7 days
105
+ });
106
+
107
+ return { accessToken, refreshToken };
108
+ });
109
+
110
+ app.post('/auth/refresh', async (request, reply) => {
111
+ const { refreshToken } = request.body;
112
+ const stored = refreshTokens.get(refreshToken);
113
+
114
+ if (!stored || stored.expires < Date.now()) {
115
+ refreshTokens.delete(refreshToken);
116
+ return reply.code(401).send({ error: 'Invalid refresh token' });
117
+ }
118
+
119
+ // Delete old token (rotation)
120
+ refreshTokens.delete(refreshToken);
121
+
122
+ const user = await db.users.findById(stored.userId);
123
+ const accessToken = app.jwt.sign({ id: user.id, role: user.role });
124
+ const newRefreshToken = randomBytes(32).toString('hex');
125
+
126
+ refreshTokens.set(newRefreshToken, {
127
+ userId: user.id,
128
+ expires: Date.now() + 7 * 24 * 60 * 60 * 1000,
129
+ });
130
+
131
+ return { accessToken, refreshToken: newRefreshToken };
132
+ });
133
+
134
+ app.post('/auth/logout', async (request, reply) => {
135
+ const { refreshToken } = request.body;
136
+ refreshTokens.delete(refreshToken);
137
+ return { success: true };
138
+ });
139
+ ```
140
+
141
+ ## Role-Based Access Control
142
+
143
+ Implement RBAC with decorators:
144
+
145
+ ```typescript
146
+ type Role = 'admin' | 'user' | 'moderator';
147
+
148
+ // Create authorization decorator
149
+ app.decorate('authorize', function (...allowedRoles: Role[]) {
150
+ return async (request, reply) => {
151
+ await request.jwtVerify();
152
+
153
+ const userRole = request.user.role as Role;
154
+ if (!allowedRoles.includes(userRole)) {
155
+ return reply.code(403).send({
156
+ error: 'Forbidden',
157
+ message: `Role '${userRole}' is not authorized for this resource`,
158
+ });
159
+ }
160
+ };
161
+ });
162
+
163
+ // Admin only route
164
+ app.get('/admin/users', {
165
+ onRequest: [app.authorize('admin')],
166
+ }, async (request) => {
167
+ return db.users.findAll();
168
+ });
169
+
170
+ // Admin or moderator
171
+ app.delete('/posts/:id', {
172
+ onRequest: [app.authorize('admin', 'moderator')],
173
+ }, async (request) => {
174
+ await db.posts.delete(request.params.id);
175
+ return { deleted: true };
176
+ });
177
+ ```
178
+
179
+ ## Permission-Based Authorization
180
+
181
+ Fine-grained permission checks:
182
+
183
+ ```typescript
184
+ interface Permission {
185
+ resource: string;
186
+ action: 'create' | 'read' | 'update' | 'delete';
187
+ }
188
+
189
+ const rolePermissions: Record<string, Permission[]> = {
190
+ admin: [
191
+ { resource: '*', action: 'create' },
192
+ { resource: '*', action: 'read' },
193
+ { resource: '*', action: 'update' },
194
+ { resource: '*', action: 'delete' },
195
+ ],
196
+ user: [
197
+ { resource: 'posts', action: 'create' },
198
+ { resource: 'posts', action: 'read' },
199
+ { resource: 'comments', action: 'create' },
200
+ { resource: 'comments', action: 'read' },
201
+ ],
202
+ };
203
+
204
+ function hasPermission(role: string, resource: string, action: string): boolean {
205
+ const permissions = rolePermissions[role] || [];
206
+ return permissions.some(
207
+ (p) =>
208
+ (p.resource === '*' || p.resource === resource) &&
209
+ p.action === action
210
+ );
211
+ }
212
+
213
+ app.decorate('checkPermission', function (resource: string, action: string) {
214
+ return async (request, reply) => {
215
+ await request.jwtVerify();
216
+
217
+ if (!hasPermission(request.user.role, resource, action)) {
218
+ return reply.code(403).send({
219
+ error: 'Forbidden',
220
+ message: `Not allowed to ${action} ${resource}`,
221
+ });
222
+ }
223
+ };
224
+ });
225
+
226
+ // Usage
227
+ app.post('/posts', {
228
+ onRequest: [app.checkPermission('posts', 'create')],
229
+ }, createPostHandler);
230
+
231
+ app.delete('/posts/:id', {
232
+ onRequest: [app.checkPermission('posts', 'delete')],
233
+ }, deletePostHandler);
234
+ ```
235
+
236
+ ## API Key / Bearer Token Authentication
237
+
238
+ Use `@fastify/bearer-auth` for API key and bearer token authentication:
239
+
240
+ ```typescript
241
+ import bearerAuth from '@fastify/bearer-auth';
242
+
243
+ const validKeys = new Set([process.env.API_KEY]);
244
+
245
+ app.register(bearerAuth, {
246
+ keys: validKeys,
247
+ errorResponse: (err) => ({
248
+ error: 'Unauthorized',
249
+ message: 'Invalid API key',
250
+ }),
251
+ });
252
+
253
+ // All routes are now protected
254
+ app.get('/api/data', async (request) => {
255
+ return { data: [] };
256
+ });
257
+ ```
258
+
259
+ For database-backed API keys with custom validation:
260
+
261
+ ```typescript
262
+ import bearerAuth from '@fastify/bearer-auth';
263
+
264
+ app.register(bearerAuth, {
265
+ auth: async (key, request) => {
266
+ const apiKey = await db.apiKeys.findByKey(key);
267
+
268
+ if (!apiKey || !apiKey.active) {
269
+ return false;
270
+ }
271
+
272
+ // Track usage (fire and forget)
273
+ db.apiKeys.recordUsage(apiKey.id, {
274
+ ip: request.ip,
275
+ timestamp: new Date(),
276
+ });
277
+
278
+ request.apiKey = apiKey;
279
+ return true;
280
+ },
281
+ errorResponse: (err) => ({
282
+ error: 'Unauthorized',
283
+ message: 'Invalid API key',
284
+ }),
285
+ });
286
+ ```
287
+
288
+ ## OAuth 2.0 Integration
289
+
290
+ Integrate with OAuth providers using @fastify/oauth2:
291
+
292
+ ```typescript
293
+ import fastifyOauth2 from '@fastify/oauth2';
294
+
295
+ app.register(fastifyOauth2, {
296
+ name: 'googleOAuth2',
297
+ scope: ['profile', 'email'],
298
+ credentials: {
299
+ client: {
300
+ id: process.env.GOOGLE_CLIENT_ID,
301
+ secret: process.env.GOOGLE_CLIENT_SECRET,
302
+ },
303
+ },
304
+ startRedirectPath: '/auth/google',
305
+ callbackUri: 'http://localhost:3000/auth/google/callback',
306
+ discovery: {
307
+ issuer: 'https://accounts.google.com',
308
+ },
309
+ });
310
+
311
+ app.get('/auth/google/callback', async (request, reply) => {
312
+ const { token } = await app.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request);
313
+
314
+ // Fetch user info from Google
315
+ const userInfo = await fetch('https://www.googleapis.com/oauth2/v2/userinfo', {
316
+ headers: { Authorization: `Bearer ${token.access_token}` },
317
+ }).then((r) => r.json());
318
+
319
+ // Find or create user
320
+ let user = await db.users.findByEmail(userInfo.email);
321
+ if (!user) {
322
+ user = await db.users.create({
323
+ email: userInfo.email,
324
+ name: userInfo.name,
325
+ provider: 'google',
326
+ providerId: userInfo.id,
327
+ });
328
+ }
329
+
330
+ // Generate JWT
331
+ const jwt = app.jwt.sign({ id: user.id, role: user.role });
332
+
333
+ // Redirect to frontend with token
334
+ return reply.redirect(`/auth/success?token=${jwt}`);
335
+ });
336
+ ```
337
+
338
+ ## Session-Based Authentication
339
+
340
+ Use @fastify/session for session management:
341
+
342
+ ```typescript
343
+ import fastifyCookie from '@fastify/cookie';
344
+ import fastifySession from '@fastify/session';
345
+ import RedisStore from 'connect-redis';
346
+ import { createClient } from 'redis';
347
+
348
+ const redisClient = createClient({ url: process.env.REDIS_URL });
349
+ await redisClient.connect();
350
+
351
+ app.register(fastifyCookie);
352
+ app.register(fastifySession, {
353
+ secret: process.env.SESSION_SECRET,
354
+ store: new RedisStore({ client: redisClient }),
355
+ cookie: {
356
+ secure: process.env.NODE_ENV === 'production',
357
+ httpOnly: true,
358
+ maxAge: 24 * 60 * 60 * 1000, // 1 day
359
+ },
360
+ });
361
+
362
+ app.post('/login', async (request, reply) => {
363
+ const { email, password } = request.body;
364
+ const user = await validateCredentials(email, password);
365
+
366
+ if (!user) {
367
+ return reply.code(401).send({ error: 'Invalid credentials' });
368
+ }
369
+
370
+ request.session.userId = user.id;
371
+ request.session.role = user.role;
372
+
373
+ return { success: true };
374
+ });
375
+
376
+ app.decorate('requireSession', async function (request, reply) {
377
+ if (!request.session.userId) {
378
+ return reply.code(401).send({ error: 'Not authenticated' });
379
+ }
380
+ });
381
+
382
+ app.get('/profile', {
383
+ onRequest: [app.requireSession],
384
+ }, async (request) => {
385
+ const user = await db.users.findById(request.session.userId);
386
+ return { user };
387
+ });
388
+
389
+ app.post('/logout', async (request, reply) => {
390
+ await request.session.destroy();
391
+ return { success: true };
392
+ });
393
+ ```
394
+
395
+ ## Resource-Based Authorization
396
+
397
+ Check ownership of resources:
398
+
399
+ ```typescript
400
+ app.decorate('checkOwnership', function (getResourceOwnerId: (request) => Promise<string>) {
401
+ return async (request, reply) => {
402
+ const ownerId = await getResourceOwnerId(request);
403
+
404
+ if (ownerId !== request.user.id && request.user.role !== 'admin') {
405
+ return reply.code(403).send({
406
+ error: 'Forbidden',
407
+ message: 'You do not own this resource',
408
+ });
409
+ }
410
+ };
411
+ });
412
+
413
+ // Check post ownership
414
+ app.put('/posts/:id', {
415
+ onRequest: [
416
+ app.authenticate,
417
+ app.checkOwnership(async (request) => {
418
+ const post = await db.posts.findById(request.params.id);
419
+ return post?.authorId;
420
+ }),
421
+ ],
422
+ }, updatePostHandler);
423
+
424
+ // Alternative: inline check
425
+ app.put('/posts/:id', {
426
+ onRequest: [app.authenticate],
427
+ }, async (request, reply) => {
428
+ const post = await db.posts.findById(request.params.id);
429
+
430
+ if (!post) {
431
+ return reply.code(404).send({ error: 'Post not found' });
432
+ }
433
+
434
+ if (post.authorId !== request.user.id && request.user.role !== 'admin') {
435
+ return reply.code(403).send({ error: 'Forbidden' });
436
+ }
437
+
438
+ return db.posts.update(post.id, request.body);
439
+ });
440
+ ```
441
+
442
+ ## Password Hashing
443
+
444
+ Use secure password hashing with argon2:
445
+
446
+ ```typescript
447
+ import { hash, verify } from '@node-rs/argon2';
448
+
449
+ async function hashPassword(password: string): Promise<string> {
450
+ return hash(password, {
451
+ memoryCost: 65536,
452
+ timeCost: 3,
453
+ parallelism: 4,
454
+ });
455
+ }
456
+
457
+ async function verifyPassword(hash: string, password: string): Promise<boolean> {
458
+ return verify(hash, password);
459
+ }
460
+
461
+ app.post('/register', async (request, reply) => {
462
+ const { email, password } = request.body;
463
+
464
+ const hashedPassword = await hashPassword(password);
465
+ const user = await db.users.create({
466
+ email,
467
+ password: hashedPassword,
468
+ });
469
+
470
+ reply.code(201);
471
+ return { id: user.id, email: user.email };
472
+ });
473
+
474
+ app.post('/login', async (request, reply) => {
475
+ const { email, password } = request.body;
476
+ const user = await db.users.findByEmail(email);
477
+
478
+ if (!user || !(await verifyPassword(user.password, password))) {
479
+ return reply.code(401).send({ error: 'Invalid credentials' });
480
+ }
481
+
482
+ const token = app.jwt.sign({ id: user.id, role: user.role });
483
+ return { token };
484
+ });
485
+ ```
486
+
487
+ ## Rate Limiting for Auth Endpoints
488
+
489
+ Protect auth endpoints from brute force. **IMPORTANT: For production security, you MUST configure rate limiting with a Redis backend.** In-memory rate limiting is not safe for distributed deployments and can be bypassed.
490
+
491
+ ```typescript
492
+ import fastifyRateLimit from '@fastify/rate-limit';
493
+ import Redis from 'ioredis';
494
+
495
+ const redis = new Redis(process.env.REDIS_URL);
496
+
497
+ // Global rate limit with Redis backend
498
+ app.register(fastifyRateLimit, {
499
+ max: 100,
500
+ timeWindow: '1 minute',
501
+ redis, // REQUIRED for production - ensures rate limiting works across all instances
502
+ });
503
+
504
+ // Stricter limit for auth endpoints
505
+ app.register(async function authRoutes(fastify) {
506
+ await fastify.register(fastifyRateLimit, {
507
+ max: 5,
508
+ timeWindow: '1 minute',
509
+ redis, // REQUIRED for production
510
+ keyGenerator: (request) => {
511
+ // Rate limit by IP + email combination
512
+ const email = request.body?.email || '';
513
+ return `${request.ip}:${email}`;
514
+ },
515
+ });
516
+
517
+ fastify.post('/login', loginHandler);
518
+ fastify.post('/register', registerHandler);
519
+ fastify.post('/forgot-password', forgotPasswordHandler);
520
+ }, { prefix: '/auth' });
521
+ ```