import { Injectable } from "@nestjs/common"; import { ThrottlerGuard } from "@nestjs/throttler"; import type { Request } from "express"; @Injectable() export class AuthThrottleGuard extends ThrottlerGuard { protected async getTracker(req: Request): Promise { // Track by IP address for failed login attempts const forwarded = req.headers["x-forwarded-for"]; const forwardedIp = Array.isArray(forwarded) ? forwarded[0] : forwarded; const ip = (typeof forwardedIp === "string" ? forwardedIp.split(",")[0]?.trim() : undefined) || (req.headers["x-real-ip"] as string | undefined) || (req.socket as any)?.remoteAddress || (req as any).ip || "unknown"; return `auth_${ip}`; } }