import { BadRequestException, Injectable } from "@nestjs/common"; import { TokensService } from "./tokens.service"; import { AuthMessage } from "../../../common/enums/message.enum"; import { NotificationQueue } from "../../notifications/queue/notification.queue"; import { UserRepository } from "../../users/repositories/user.repository"; import { PasswordService } from "../../utils/services/password.service"; import { ChangePasswordDto } from "../DTO/change-password.dto"; import { LoginDto } from "../DTO/login.dto"; @Injectable() export class AuthService { constructor( private readonly notificationQueue: NotificationQueue, private readonly userRepository: UserRepository, private readonly tokensService: TokensService, private readonly passwordService: PasswordService, ) {} //============================================== //============================================== async login(loginDto: LoginDto) { const { email, password } = loginDto; const user = await this.checkUserLoginCredentialWithEmail(email, password); const tokens = await this.tokensService.generateTokens(user); await this.notificationQueue.addLoginNotification(user.id, { userEmail: email, userName: user.displayName, }); return { message: AuthMessage.PASSWORD_LOGIN_SUCCESS, ...tokens, }; } //============================================== //============================================== async refreshToken(oldRefreshToken: string) { return this.tokensService.refreshToken(oldRefreshToken); } //============================================== //============================================== async logout(userId: string) { await this.tokensService.invalidateRefreshToken(userId); return { message: AuthMessage.LOGOUT_SUCCESS, }; } //============================================== //============================================== async changePassword(userId: string, changePasswordDto: ChangePasswordDto) { const user = await this.userRepository.findOne({ id: userId, deletedAt: null }); if (!user) throw new BadRequestException(AuthMessage.USER_NOT_FOUND); const passCompare = await this.passwordService.comparePasswords(changePasswordDto.oldPassword, user.password); if (!passCompare) throw new BadRequestException(AuthMessage.CURRENT_PASSWORD_INVALID); if (changePasswordDto.newPassword !== changePasswordDto.repeatPassword) throw new BadRequestException(AuthMessage.INVALID_REPEAT_PASSWORD); const hashedPassword = await this.passwordService.hashPassword(changePasswordDto.newPassword); await this.userRepository.nativeUpdate({ id: userId }, { password: hashedPassword }); return { message: AuthMessage.PASSWORD_CHANGED_SUCCESSFULLY, }; } //============================================== private async checkUserLoginCredentialWithEmail(email: string, password: string) { const user = await this.userRepository.findOne({ emailAddress: email, isActive: true }); if (!user) throw new BadRequestException(AuthMessage.INVALID_PASSWORD); const passCompare = await this.passwordService.comparePasswords(password, user.password); if (!passCompare) throw new BadRequestException(AuthMessage.INVALID_PASSWORD); return user; } }