Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Change auth controller login routes
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 3, 2020
1 parent fb46eeb commit c797457
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
File renamed without changes.
18 changes: 2 additions & 16 deletions src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class AuthController {
);
}

@Post('totp-login')
@Post('login/totp')
@RateLimit({
points: 10,
duration: 60,
Expand All @@ -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<TokenResponse> {
return this.authService.loginWithEmailToken(ip, userAgent, token);
}

@Post('token-login')
@Post('login/token')
@RateLimit({
points: 10,
duration: 60,
Expand Down
44 changes: 23 additions & 21 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,7 @@ export class AuthService {
_data: RegisterDto,
): Promise<Expose<users>> {
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 } } },
});
Expand All @@ -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: {
Expand All @@ -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);
Expand Down

0 comments on commit c797457

Please sign in to comment.