diff --git a/src/plugins/rateLimit.ts b/src/plugins/rateLimit.ts index 38869f4..41bb9f2 100644 --- a/src/plugins/rateLimit.ts +++ b/src/plugins/rateLimit.ts @@ -29,6 +29,7 @@ const rateLimitPlugin: FastifyPluginAsync = async (app: FastifyInstance) => { app.decorate('rateLimitOptions', options); await app.register(rateLimit, { + global: false, max: options.apiGuest.max, timeWindow: options.apiGuest.timeWindow, keyGenerator: (req) => { diff --git a/src/routes/auth.ts b/src/routes/auth.ts index 2abecae..9b351ca 100644 --- a/src/routes/auth.ts +++ b/src/routes/auth.ts @@ -70,10 +70,11 @@ const resetPasswordSchema = { export async function authRoutes(app: FastifyInstance) { const authService = new AuthService(app.db); + const { rateLimitOptions } = app; app.post( '/register', - { schema: registerSchema }, + { schema: registerSchema, config: { rateLimit: rateLimitOptions.register } }, async (req, reply) => { const body = req.body as { email: string; password: string; nickname: string }; const { userId, verificationCode } = await authService.register(body); @@ -88,7 +89,7 @@ export async function authRoutes(app: FastifyInstance) { app.post( '/login', - { schema: loginSchema }, + { schema: loginSchema, config: { rateLimit: rateLimitOptions.login } }, async (req, reply) => { const body = req.body as { email: string; password: string }; const userAgent = req.headers['user-agent']; @@ -107,7 +108,7 @@ export async function authRoutes(app: FastifyInstance) { app.post( '/logout', - { schema: logoutSchema }, + { schema: logoutSchema, config: { rateLimit: rateLimitOptions.apiGuest } }, async (req, reply) => { const body = req.body as { refreshToken: string }; await authService.logout(body.refreshToken); @@ -117,7 +118,7 @@ export async function authRoutes(app: FastifyInstance) { app.post( '/refresh', - { schema: refreshTokenSchema }, + { schema: refreshTokenSchema, config: { rateLimit: rateLimitOptions.apiGuest } }, async (req, reply) => { const body = req.body as { refreshToken: string }; const userAgent = req.headers['user-agent']; @@ -135,7 +136,7 @@ export async function authRoutes(app: FastifyInstance) { app.post( '/verify-email', - { schema: verifyEmailSchema }, + { schema: verifyEmailSchema, config: { rateLimit: rateLimitOptions.verifyEmail } }, async (req, reply) => { const body = req.body as { userId: string; code: string }; await authService.verifyEmail(body.userId, body.code); @@ -145,7 +146,7 @@ export async function authRoutes(app: FastifyInstance) { app.post( '/forgot-password', - { schema: forgotPasswordSchema }, + { schema: forgotPasswordSchema, config: { rateLimit: rateLimitOptions.forgotPassword } }, async (req, reply) => { const body = req.body as { email: string }; await authService.forgotPassword(body.email); @@ -157,7 +158,7 @@ export async function authRoutes(app: FastifyInstance) { app.post( '/reset-password', - { schema: resetPasswordSchema }, + { schema: resetPasswordSchema, config: { rateLimit: rateLimitOptions.forgotPassword } }, async (req, reply) => { const body = req.body as { token: string; newPassword: string }; await authService.resetPassword(body.token, body.newPassword);