Skip to content

Commit 58c2f86

Browse files
committed
create ThrottleLoginRequest decorator and apply to /auth/create-instrument-token as well
1 parent c7adf42 commit 58c2f86

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

apps/api/src/auth/auth.controller.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { $NumberLike } from '@douglasneuroinformatics/libjs';
21
import { CurrentUser } from '@douglasneuroinformatics/libnest';
32
import type { RequestUser } from '@douglasneuroinformatics/libnest';
43
import { Body, Controller, Get, HttpCode, HttpStatus, Post } from '@nestjs/common';
54
import { ApiOperation } from '@nestjs/swagger';
6-
import { Throttle } from '@nestjs/throttler';
75
import { $LoginCredentials } from '@opendatacapture/schemas/auth';
8-
import z from 'zod/v4';
96

107
import { RouteAccess } from '@/core/decorators/route-access.decorator.js';
8+
import { ThrottleLoginRequest } from '@/core/decorators/throttle-login-request.decorator.js';
119

1210
import { AuthService } from './auth.service.js';
1311

@@ -18,7 +16,7 @@ export class AuthController {
1816
@Get('create-instrument-token')
1917
@HttpCode(HttpStatus.OK)
2018
@RouteAccess({ action: 'create', subject: 'Instrument' })
21-
@Throttle({ long: { limit: 50, ttl: 60_000 } })
19+
@ThrottleLoginRequest()
2220
async getCreateInstrumentToken(@CurrentUser() currentUser: RequestUser): Promise<{ accessToken: string }> {
2321
return this.authService.getCreateInstrumentToken(currentUser);
2422
}
@@ -27,12 +25,7 @@ export class AuthController {
2725
@HttpCode(HttpStatus.OK)
2826
@Post('login')
2927
@RouteAccess('public')
30-
@Throttle({
31-
long: {
32-
limit: $NumberLike.pipe(z.number().int().positive()).default(50).parse(process.env.LOGIN_REQUEST_THROTTLER_LIMIT),
33-
ttl: $NumberLike.pipe(z.number().int().positive()).default(60_000).parse(process.env.LOGIN_REQUEST_THROTTLER_TTL)
34-
}
35-
})
28+
@ThrottleLoginRequest()
3629
async login(@Body() credentials: $LoginCredentials): Promise<{ accessToken: string }> {
3730
return this.authService.login(credentials);
3831
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { $NumberLike } from '@douglasneuroinformatics/libjs';
2+
import { applyDecorators } from '@nestjs/common';
3+
import { Throttle } from '@nestjs/throttler';
4+
import z from 'zod/v4';
5+
6+
// we cannot inject the config service here, so this needs to be parsed manually
7+
8+
const LOGIN_REQUEST_THROTTLER_LIMIT = $NumberLike
9+
.pipe(z.number().int().positive())
10+
.default(50)
11+
.parse(process.env.LOGIN_REQUEST_THROTTLER_LIMIT);
12+
13+
const LOGIN_REQUEST_THROTTLER_TTL = $NumberLike
14+
.pipe(z.number().int().positive())
15+
.default(60_000)
16+
.parse(process.env.LOGIN_REQUEST_THROTTLER_TTL);
17+
18+
export function ThrottleLoginRequest() {
19+
return applyDecorators(
20+
Throttle({
21+
long: {
22+
limit: LOGIN_REQUEST_THROTTLER_LIMIT,
23+
ttl: LOGIN_REQUEST_THROTTLER_TTL
24+
}
25+
})
26+
);
27+
}

0 commit comments

Comments
 (0)