fix: auth otp flow + remove generated

This commit is contained in:
SauravDhakal
2026-04-05 16:19:19 +05:45
parent 4905c6f1d1
commit ab8b2ef353
27 changed files with 340 additions and 8425 deletions

View File

@@ -20,7 +20,7 @@ import { Public } from './decorators';
@Controller('auth')
@Public()
export class AuthController {
constructor(private readonly authService: AuthService) {}
constructor(private readonly authService: AuthService) { }
@ApiOperation({ summary: 'User login' })
@HttpCode(HttpStatus.OK)
@@ -46,12 +46,12 @@ export class AuthController {
async register(@Body() body: RegisterUserRequestDTO): Promise<string> {
await this.authService.register(body);
return 'Registered successfully. Login to continue.';
return 'Check your email for OTP';
}
logout() {}
logout() { }
forgotPassword() {}
forgotPassword() { }
regenTokens() {}
regenTokens() { }
}

View File

@@ -1,4 +1,4 @@
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
import { ConflictException, Inject, Injectable, UnauthorizedException } from '@nestjs/common';
import { Public } from './decorators';
import { LoginUserRequestDTO, RegisterUserRequestDTO } from './dto';
import * as bcrypt from 'bcrypt';
@@ -17,26 +17,75 @@ export class AuthService {
@InjectQueue('mail') private readonly mailQueue: Queue
) { }
// Generate OTP
async register(dto: RegisterUserRequestDTO) {
const hashedPassword = await bcrypt.hash(dto.password, 10);
await this.userService.createUserWithPassword({
...dto,
password: hashedPassword,
});
const [userExists, otpExists] = await Promise.all([
this.userService.findByEmail(dto.email),
this.userService.findByEmailInOTP(dto.email),
])
this.mailQueue.add('send-welcome-email', {
email: dto.email
if (userExists)
throw new ConflictException("User with this email already exists");
else if (otpExists) {
/* *
* If OTP was last generated more than 2 minutes ago, regen.
* Else, do nothing
* */
const now = Number(new Date()) / 1000;
const generatedOn = Number(otpExists.generatedOn) / 1000;
if (generatedOn + (60 * 2) > now) {
return;
}
}
const otp = this.genOtp()
await this.userService.updateOTPByEmail(dto.email, otp);
this.mailQueue.add('send-register-otp-email', {
email: dto.email,
otp: otp
}, {
attempts: 3,
backoff: {
type: "exponential",
delay: 3000,
delay: 3000
},
removeOnComplete: true, // clean up Redis after success
removeOnFail: false,
})
return true;
// const hashedPassword = await bcrypt.hash(dto.password, 10);
// await this.userService.createUserWithPassword({
// ...dto,
// password: hashedPassword,
// });
//
// this.mailQueue.add('send-welcome-email', {
// email: dto.email
// }, {
// attempts: 3,
// backoff: {
// type: "exponential",
// delay: 3000,
// },
// removeOnComplete: true, // clean up Redis after success
// removeOnFail: false,
// })
//
// return true;
}
// Validate OTP
async validateOtp() {
}
// Complete rest of singup process
async completeSignup() {
}
async login(dto: LoginUserRequestDTO) {
@@ -86,4 +135,9 @@ export class AuthService {
return { accessToken, refreshToken };
}
private genOtp() {
return 123456;
}
}