|
|
@@ -1,4 +1,12 @@
|
|
|
-import { Injectable, NotFoundException, HttpException, HttpStatus, BadRequestException } from '@nestjs/common'
|
|
|
+import {
|
|
|
+ Injectable,
|
|
|
+ NotFoundException,
|
|
|
+ HttpException,
|
|
|
+ HttpStatus,
|
|
|
+ BadRequestException,
|
|
|
+ Logger,
|
|
|
+ UnauthorizedException
|
|
|
+} from '@nestjs/common'
|
|
|
import { Repository, UpdateResult } from 'typeorm'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { Users } from './entities/users.entity'
|
|
|
@@ -7,15 +15,96 @@ import { UserDto } from './dto/user.dto'
|
|
|
import { UserProfileDto } from './dto/user-profile.dto'
|
|
|
import { UserUpdateDto } from './dto/user-update.dto'
|
|
|
import { HashingService } from '../shared/hashing/hashing.service'
|
|
|
+import { MailerService } from 'src/shared/mailer/mailer.service'
|
|
|
+import { RegisterUserDto } from 'src/iam/dto/register-user.dto'
|
|
|
+import { LoginDto } from 'src/iam/dto/login.dto'
|
|
|
+import { JwtService } from '@nestjs/jwt'
|
|
|
+import { ConfigService } from '@nestjs/config'
|
|
|
+import { JWTPayload } from '../iam/interfaces/jwt-payload.interface'
|
|
|
+import { UtilsService } from 'src/shared/utils/utils.service'
|
|
|
+import { ForgotPasswordDto } from 'src/iam/dto/forgot-password.dto'
|
|
|
+import { ChangePasswordDto } from 'src/iam/dto/change-password.dto'
|
|
|
|
|
|
@Injectable()
|
|
|
export class UsersService {
|
|
|
constructor(
|
|
|
@InjectRepository(Users)
|
|
|
private readonly userRepository: Repository<Users>,
|
|
|
- private readonly hashingService: HashingService
|
|
|
+ private readonly hashingService: HashingService,
|
|
|
+ private readonly mailerService: MailerService,
|
|
|
+ private readonly jwtService: JwtService,
|
|
|
+ private readonly configService: ConfigService,
|
|
|
+ private readonly utilsService: UtilsService
|
|
|
) {}
|
|
|
|
|
|
+ public async register(registerUserDto: RegisterUserDto): Promise<IUsers> {
|
|
|
+ registerUserDto.password = await this.hashingService.hash(registerUserDto.password)
|
|
|
+
|
|
|
+ this.sendMailRegisterUser(registerUserDto)
|
|
|
+
|
|
|
+ return this.create(registerUserDto)
|
|
|
+ }
|
|
|
+
|
|
|
+ private sendMailRegisterUser(user): void {
|
|
|
+ try {
|
|
|
+ this.mailerService.sendMail({
|
|
|
+ to: user.email,
|
|
|
+ from: 'from@example.com',
|
|
|
+ subject: 'Registration successful ✔',
|
|
|
+ text: 'Registration successful!',
|
|
|
+ template: 'index',
|
|
|
+ context: {
|
|
|
+ title: 'Registration successfully',
|
|
|
+ description: "You did it! You registered!, You're successfully registered.✔",
|
|
|
+ nameUser: user.name
|
|
|
+ }
|
|
|
+ })
|
|
|
+ Logger.log('[MailService] User Registration: Send Mail successfully!')
|
|
|
+ } catch (err) {
|
|
|
+ Logger.error('[MailService] User Registration: Send Mail failed!', err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public async findUserByEmail(loginDto: LoginDto): Promise<IUsers> {
|
|
|
+ return await this.findByEmail(loginDto.email)
|
|
|
+ }
|
|
|
+
|
|
|
+ public async login(loginDto: LoginDto): Promise<any> {
|
|
|
+ try {
|
|
|
+ const user = await this.findUserByEmail(loginDto)
|
|
|
+ if (!user) {
|
|
|
+ throw new UnauthorizedException('User does not exists')
|
|
|
+ }
|
|
|
+
|
|
|
+ const passwordIsValid = await this.hashingService.compare(loginDto.password, user.password)
|
|
|
+
|
|
|
+ if (!passwordIsValid) {
|
|
|
+ throw new UnauthorizedException('Authentication failed. Wrong password')
|
|
|
+ }
|
|
|
+
|
|
|
+ return await this.signToken({
|
|
|
+ name: user.name,
|
|
|
+ email: user.email,
|
|
|
+ id: user.id
|
|
|
+ })
|
|
|
+ } catch (err) {
|
|
|
+ throw new HttpException(err, HttpStatus.BAD_REQUEST)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private async signToken(payload: JWTPayload): Promise<any> {
|
|
|
+ const accessToken = await this.jwtService.signAsync(payload)
|
|
|
+
|
|
|
+ return {
|
|
|
+ sub: payload.id,
|
|
|
+ expiresIn: this.configService.get<string>('JWT_ACCESS_TOKEN_TTL'),
|
|
|
+ audience: this.configService.get<string>('JWT_TOKEN_AUDIENCE'),
|
|
|
+ issuer: this.configService.get<string>('JWT_TOKEN_ISSUER'),
|
|
|
+ accessToken: accessToken,
|
|
|
+ user: payload
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public async findAll(): Promise<Users[]> {
|
|
|
return await this.userRepository.find()
|
|
|
}
|
|
|
@@ -106,4 +195,61 @@ export class UsersService {
|
|
|
const user = await this.findById(id)
|
|
|
await this.userRepository.remove(user)
|
|
|
}
|
|
|
+
|
|
|
+ public async forgotPassword(forgotPasswordDto: ForgotPasswordDto): Promise<any> {
|
|
|
+ const userUpdate = await this.userRepository.findOneBy({
|
|
|
+ email: forgotPasswordDto.email
|
|
|
+ })
|
|
|
+ const passwordRand = this.utilsService.generatePassword()
|
|
|
+ userUpdate.password = await this.hashingService.hash(passwordRand)
|
|
|
+
|
|
|
+ this.sendMailForgotPassword(userUpdate.email, passwordRand)
|
|
|
+
|
|
|
+ return await this.userRepository.save(userUpdate)
|
|
|
+ }
|
|
|
+
|
|
|
+ private sendMailForgotPassword(email, password): void {
|
|
|
+ try {
|
|
|
+ this.mailerService.sendMail({
|
|
|
+ to: email,
|
|
|
+ from: 'from@example.com',
|
|
|
+ subject: 'Forgot Password successful ✔',
|
|
|
+ text: 'Forgot Password successful!',
|
|
|
+ template: 'index',
|
|
|
+ context: {
|
|
|
+ title: 'Forgot Password successful!',
|
|
|
+ description: 'Request Reset Password Successfully! ✔, This is your new password: ' + password
|
|
|
+ }
|
|
|
+ })
|
|
|
+ Logger.log('[MailService] Forgot Password: Send Mail successfully!')
|
|
|
+ } catch (err) {
|
|
|
+ Logger.error('[MailService] Forgot Password: Send Mail Failed!', err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public async changePassword(changePasswordDto: ChangePasswordDto): Promise<any> {
|
|
|
+ this.sendMailChangePassword(changePasswordDto)
|
|
|
+
|
|
|
+ return await this.updateByPassword(changePasswordDto.email, changePasswordDto.password)
|
|
|
+ }
|
|
|
+
|
|
|
+ private sendMailChangePassword(user): void {
|
|
|
+ try {
|
|
|
+ this.mailerService.sendMail({
|
|
|
+ to: user.email,
|
|
|
+ from: 'from@example.com',
|
|
|
+ subject: 'Change Password successful ✔',
|
|
|
+ text: 'Change Password successful!',
|
|
|
+ template: 'index',
|
|
|
+ context: {
|
|
|
+ title: 'Change Password successful!',
|
|
|
+ description: 'Change Password Successfully! ✔, This is your new password: ' + user.password,
|
|
|
+ nameUser: user.name
|
|
|
+ }
|
|
|
+ })
|
|
|
+ Logger.log('[MailService] Change Password: Send Mail successfully!')
|
|
|
+ } catch (err) {
|
|
|
+ Logger.error('[MailService] Change Password: Send Mail Failed!', err)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|