From c797457bb21331925d08008e8caef1ef32b31cf4 Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Tue, 3 Nov 2020 17:45:36 +0530 Subject: [PATCH] :recycle: Change auth controller login routes --- api.http => http/auth/register.http | 0 src/modules/auth/auth.controller.ts | 18 ++---------- src/modules/auth/auth.service.ts | 44 +++++++++++++++-------------- 3 files changed, 25 insertions(+), 37 deletions(-) rename api.http => http/auth/register.http (100%) diff --git a/api.http b/http/auth/register.http similarity index 100% rename from api.http rename to http/auth/register.http diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 06974f66a..0a3620c4b 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -144,7 +144,7 @@ export class AuthController { ); } - @Post('totp-login') + @Post('login/totp') @RateLimit({ points: 10, duration: 60, @@ -158,21 +158,7 @@ export class AuthController { return this.authService.loginWithTotp(ip, userAgent, data.token, data.code); } - @Get('token-login') - @RateLimit({ - points: 10, - duration: 60, - errorMessage: 'Wait for 60 seconds before trying to login again', - }) - async emailTokenLoginGet( - @Query('token') token: string, - @Ip() ip: string, - @Headers('User-Agent') userAgent: string, - ): Promise { - return this.authService.loginWithEmailToken(ip, userAgent, token); - } - - @Post('token-login') + @Post('login/token') @RateLimit({ points: 10, duration: 60, diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 89b067a2d..e53f8ac72 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -127,24 +127,7 @@ export class AuthService { _data: RegisterDto, ): Promise> { const { email, ...data } = _data; - data.name = data.name - .split(' ') - .map((word, index) => - index === 0 || index === data.name.split(' ').length - ? (word.charAt(0) ?? '').toUpperCase() + - (word.slice(1) ?? '').toLowerCase() - : word, - ) - .join(' '); const emailSafe = safeEmail(email); - const ignorePwnedPassword = !!data.ignorePwnedPassword; - delete data.ignorePwnedPassword; - if (data.password) - data.password = await this.hashAndValidatePassword( - data.password, - ignorePwnedPassword, - ); - const testUser = await this.prisma.users.findFirst({ where: { emails: { some: { emailSafe } } }, }); @@ -153,6 +136,24 @@ export class AuthService { 'A user with this email already exists', HttpStatus.CONFLICT, ); + const ignorePwnedPassword = !!data.ignorePwnedPassword; + delete data.ignorePwnedPassword; + + if (data.name) + data.name = data.name + .split(' ') + .map((word, index) => + index === 0 || index === data.name.split(' ').length + ? (word.charAt(0) ?? '').toUpperCase() + + (word.slice(1) ?? '').toLowerCase() + : word, + ) + .join(' '); + if (data.password) + data.password = await this.hashAndValidatePassword( + data.password, + ignorePwnedPassword, + ); const user = await this.prisma.users.create({ data: { @@ -163,10 +164,11 @@ export class AuthService { }, include: { emails: { select: { id: true } } }, }); - await this.prisma.users.update({ - where: { id: user.id }, - data: { prefersEmail: { connect: { id: user.emails[0]?.id } } }, - }); + if (user.emails[0]?.id) + await this.prisma.users.update({ + where: { id: user.id }, + data: { prefersEmail: { connect: { id: user.emails[0].id } } }, + }); await this.sendEmailVerification(email); await this.approvedSubnetsService.approveNewSubnet(user.id, ipAddress); return this.prisma.expose(user);