|
1 | 1 | import * as bcrypt from 'bcryptjs';
|
2 |
| -import { Inject, Injectable } from '@nestjs/common'; |
| 2 | +import { HttpStatus, Inject, Injectable } from '@nestjs/common'; |
3 | 3 | import { JwtService } from '@nestjs/jwt';
|
4 | 4 | import { Credentials, OAuth2Client } from 'google-auth-library';
|
5 | 5 | import { ClientProxy } from '@nestjs/microservices';
|
6 |
| -import { AuthDto, AuthIdDto, RefreshTokenDto } from './dto'; |
| 6 | +import { |
| 7 | + AuthDto, |
| 8 | + AuthIdDto, |
| 9 | + RefreshTokenDto, |
| 10 | + ResetPasswordDto, |
| 11 | + ResetPasswordRequestDto, |
| 12 | +} from './dto'; |
7 | 13 | import { HttpService } from '@nestjs/axios';
|
8 | 14 | import { RpcException } from '@nestjs/microservices';
|
9 | 15 | import { firstValueFrom } from 'rxjs';
|
10 | 16 | import axios, { AxiosResponse } from 'axios';
|
11 | 17 | import { Token, TokenPayload } from './interfaces';
|
12 | 18 | import { AccountProvider } from './constants/account-provider.enum';
|
| 19 | +import * as nodemailer from 'nodemailer'; |
13 | 20 |
|
14 | 21 | const SALT_ROUNDS = 10;
|
15 | 22 |
|
@@ -141,6 +148,102 @@ export class AppService {
|
141 | 148 | }
|
142 | 149 | }
|
143 | 150 |
|
| 151 | + public async generateResetPasswordRequest(dto: ResetPasswordRequestDto): Promise<boolean> { |
| 152 | + const user = await firstValueFrom( |
| 153 | + this.userClient.send( |
| 154 | + { |
| 155 | + cmd: 'get-user-by-email', |
| 156 | + }, |
| 157 | + dto.email, |
| 158 | + ), |
| 159 | + ); |
| 160 | + |
| 161 | + if (!user) { |
| 162 | + throw new RpcException('User not found'); |
| 163 | + } |
| 164 | + |
| 165 | + const resetToken = this.jwtService.sign( |
| 166 | + { userId: user._id.toString(), email: dto.email, type: 'reset-password' }, |
| 167 | + { |
| 168 | + secret: process.env.JWT_SECRET, |
| 169 | + expiresIn: '1hr', |
| 170 | + }, |
| 171 | + ); |
| 172 | + |
| 173 | + // Send reset password email |
| 174 | + await this.sendResetEmail(user.email, resetToken); |
| 175 | + |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + public async resetPassword(dto: ResetPasswordDto): Promise<boolean> { |
| 180 | + const { userId, email } = await this.validatePasswordResetToken(dto.token); |
| 181 | + |
| 182 | + const hashedPassword = await bcrypt.hash(dto.password, SALT_ROUNDS); |
| 183 | + |
| 184 | + const response = await firstValueFrom( |
| 185 | + this.userClient.send( |
| 186 | + { cmd: 'update-user-password' }, |
| 187 | + { id: userId, password: hashedPassword }, |
| 188 | + ), |
| 189 | + ); |
| 190 | + |
| 191 | + if (!response) { |
| 192 | + throw new RpcException('Error resetting password'); |
| 193 | + } |
| 194 | + |
| 195 | + return true; |
| 196 | + } |
| 197 | + |
| 198 | + public async validatePasswordResetToken(token: string): Promise<any> { |
| 199 | + try { |
| 200 | + const decoded = this.jwtService.verify(token, { |
| 201 | + secret: process.env.JWT_SECRET, |
| 202 | + }); |
| 203 | + const { userId, email, type } = decoded; |
| 204 | + if (type !== 'reset-password') { |
| 205 | + throw new RpcException({ |
| 206 | + statusCode: HttpStatus.UNAUTHORIZED, |
| 207 | + message: 'Unauthorized: Invalid or expired password reset token', |
| 208 | + }); |
| 209 | + } |
| 210 | + return { userId, email }; |
| 211 | + } catch (err) { |
| 212 | + throw new RpcException({ |
| 213 | + statusCode: HttpStatus.UNAUTHORIZED, |
| 214 | + message: 'Unauthorized: Invalid or expired password reset token', |
| 215 | + }); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + private async sendResetEmail(email: string, token: string) { |
| 220 | + const resetUrl = `${process.env.FRONTEND_URL}/reset-password?token=${token}`; // To change next time |
| 221 | + |
| 222 | + const transporter = nodemailer.createTransport({ |
| 223 | + service: 'gmail', |
| 224 | + host: 'smtp.gmail.com', |
| 225 | + port: 465, |
| 226 | + secure: true, |
| 227 | + auth: { |
| 228 | + user: process.env.NODEMAILER_GMAIL_USER, |
| 229 | + pass: process.env.NODEMAILER_GMAIL_PASSWORD, |
| 230 | + }, |
| 231 | + }); |
| 232 | + |
| 233 | + const mailOptions = { |
| 234 | + |
| 235 | + to: email, |
| 236 | + subject: 'Password Reset for PeerPrep', |
| 237 | + text: `Click here to reset your password: ${resetUrl}`, |
| 238 | + }; |
| 239 | + |
| 240 | + transporter.sendMail(mailOptions, (error, info) => { |
| 241 | + if (error) { |
| 242 | + throw new RpcException(`Error sending reset email: ${error.message}`); |
| 243 | + } |
| 244 | + }); |
| 245 | + } |
| 246 | + |
144 | 247 | public async validateAccessToken(accessToken: string): Promise<any> {
|
145 | 248 | try {
|
146 | 249 | const decoded = this.jwtService.verify(accessToken, {
|
@@ -412,7 +515,7 @@ export class AppService {
|
412 | 515 | return {
|
413 | 516 | ...response.data,
|
414 | 517 | email: emailResponse.data.find((email) => email.primary)?.email,
|
415 |
| - } |
| 518 | + }; |
416 | 519 | } catch (error) {
|
417 | 520 | throw new RpcException(
|
418 | 521 | `Unable to retrieve Github user profile: ${error.message}`,
|
|
0 commit comments