nestjs-auth-kit
Version:
A modular and flexible authentication kit for NestJS with JWT, social login, OTP, and password reset.
30 lines (25 loc) • 1 kB
text/typescript
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from '../entities/user.entity';
import { OtpService } from './otp.service';
()
export class ForgotPasswordService {
constructor(
(User)
private readonly userRepository: Repository<User>,
private readonly otpService: OtpService,
) {}
async resetPassword(email: string, otp: string, newPassword: string): Promise<void> {
const isValidOtp = await this.otpService.verifyOtp(email, otp);
if (!isValidOtp) {
throw new Error('Invalid or expired OTP.');
}
const user = await this.userRepository.findOne({ where: { email } });
if (!user) {
throw new Error('User not found.');
}
user.password = newPassword; // Hash the password before saving in a real app
await this.userRepository.save(user);
}
}