mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-26 06:09:41 +00:00
add login flow for waiting list demo app
This commit is contained in:
@ -18,8 +18,10 @@ import {
|
||||
ForgetPasswordRequestDto,
|
||||
LoginRequestDto,
|
||||
SendForgetPasswordOtpRequestDto,
|
||||
SendLoginOtpRequestDto,
|
||||
SetEmailRequestDto,
|
||||
setJuniorPasswordRequestDto,
|
||||
VerifyLoginOtpRequestDto,
|
||||
VerifyUserRequestDto,
|
||||
} from '../dtos/request';
|
||||
import { GrantType, Roles } from '../enums';
|
||||
@ -325,6 +327,39 @@ export class AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
async sendLoginOtp({ email }: SendLoginOtpRequestDto) {
|
||||
const user = await this.userService.findOrCreateByEmail(email);
|
||||
this.logger.log(`Sending login OTP to ${email}`);
|
||||
return this.otpService.generateAndSendOtp({
|
||||
recipient: email,
|
||||
scope: OtpScope.LOGIN,
|
||||
otpType: OtpType.EMAIL,
|
||||
userId: user.id,
|
||||
});
|
||||
}
|
||||
|
||||
async verifyLoginOtp({ email, otp }: VerifyLoginOtpRequestDto): Promise<[ILoginResponse, User]> {
|
||||
const user = await this.userService.findUserOrThrow({ email });
|
||||
|
||||
this.logger.log(`Verifying login OTP for ${email}`);
|
||||
const isOtpValid = await this.otpService.verifyOtp({
|
||||
otpType: OtpType.EMAIL,
|
||||
scope: OtpScope.LOGIN,
|
||||
userId: user.id,
|
||||
value: otp,
|
||||
});
|
||||
|
||||
if (!isOtpValid) {
|
||||
this.logger.error(`Invalid OTP for user with email ${email}`);
|
||||
throw new BadRequestException('OTP.INVALID_OTP');
|
||||
}
|
||||
|
||||
this.logger.log(`Login OTP verified successfully for ${email}`);
|
||||
|
||||
const token = await this.generateAuthToken(user);
|
||||
return [token, user];
|
||||
}
|
||||
|
||||
logout(req: Request) {
|
||||
this.logger.log('Logging out');
|
||||
const accessToken = req.headers.authorization?.split(' ')[1] as string;
|
||||
|
Reference in New Issue
Block a user