add email account validation

This commit is contained in:
andres
2023-06-13 01:39:33 +02:00
parent 59b4eb582e
commit 6c62d87ee6
40 changed files with 578 additions and 6453 deletions

View File

@@ -1,34 +1,26 @@
import { v4 as uuidv4 } from 'uuid';
import {
CreateUserInput,
EntityWithPaginationType,
User,
UserViewType,
} from '../../../types/types';
import { addHours } from 'date-fns';
import { Injectable } from '@nestjs/common';
import jwt from 'jsonwebtoken';
import { UsersRepository } from '../infrastructure/users.repository';
import * as bcrypt from 'bcrypt';
import { v4 as uuidv4 } from 'uuid'
import { CreateUserInput, EntityWithPaginationType, User, UserViewType } from '../../../types/types'
import { addHours } from 'date-fns'
import { Injectable } from '@nestjs/common'
import jwt from 'jsonwebtoken'
import { UsersRepository } from '../infrastructure/users.repository'
import * as bcrypt from 'bcrypt'
import { MailerService } from '@nestjs-modules/mailer'
@Injectable()
export class UsersService {
constructor(private usersRepository: UsersRepository) {}
constructor(private usersRepository: UsersRepository, private emailService: MailerService) {}
async getUsers(page: number, pageSize: number, searchNameTerm: string) {
return await this.usersRepository.getUsers(page, pageSize, searchNameTerm);
async getUsers(page: number, pageSize: number, searchNameTerm: string, searchEmailTerm: string) {
return await this.usersRepository.getUsers(page, pageSize, searchNameTerm, searchEmailTerm)
}
async getUserById(id: string) {
return await this.usersRepository.findUserById(id);
return await this.usersRepository.findUserById(id)
}
async createUser(
name: string,
password: string,
email: string,
): Promise<UserViewType | null> {
const passwordHash = await this._generateHash(password);
async createUser(name: string, password: string, email: string): Promise<UserViewType | null> {
const passwordHash = await this._generateHash(password)
const newUser: CreateUserInput = {
name: name || email.split('@')[0],
email: email,
@@ -36,37 +28,51 @@ export class UsersService {
verificationToken: uuidv4(),
verificationTokenExpiry: addHours(new Date(), 24),
isEmailVerified: false,
};
const createdUser = await this.usersRepository.createUser(newUser);
if (!createdUser) {
return null;
}
const createdUser = await this.usersRepository.createUser(newUser)
if (!createdUser) {
return null
}
try {
await this.emailService.sendMail({
from: 'andrii <andrii@andrii.es>',
to: createdUser.email,
text: 'hello and welcome',
subject: 'E-mail confirmation ',
})
} catch (e) {
console.log(e)
}
return {
id: createdUser.id,
name: createdUser.name,
email: createdUser.email,
};
}
}
async deleteUserById(id: string): Promise<boolean> {
return await this.usersRepository.deleteUserById(id);
return await this.usersRepository.deleteUserById(id)
}
async deleteAllUsers(): Promise<boolean> {
return await this.usersRepository.deleteAllUsers()
}
async addRevokedToken(token: string) {
const secretKey = process.env.JWT_SECRET_KEY;
if (!secretKey) throw new Error('JWT_SECRET_KEY is not defined');
const secretKey = process.env.JWT_SECRET_KEY
if (!secretKey) throw new Error('JWT_SECRET_KEY is not defined')
try {
const decoded: any = jwt.verify(token, secretKey);
return this.usersRepository.revokeToken(decoded.userId, token);
const decoded: any = jwt.verify(token, secretKey)
return this.usersRepository.revokeToken(decoded.userId, token)
} catch (e) {
console.log('Decoding error: e');
return null;
console.log(`Decoding error: ${e}`)
return null
}
}
private async _generateHash(password: string) {
return await bcrypt.hash(password, 10);
return await bcrypt.hash(password, 10)
}
}
@@ -75,13 +81,14 @@ export interface IUsersRepository {
page: number,
pageSize: number,
searchNameTerm: string,
): Promise<EntityWithPaginationType<UserViewType>>;
searchEmailTerm: string
): Promise<EntityWithPaginationType<UserViewType>>
createUser(newUser: CreateUserInput): Promise<User | null>;
createUser(newUser: CreateUserInput): Promise<User | null>
deleteUserById(id: string): Promise<boolean>;
deleteUserById(id: string): Promise<boolean>
findUserById(id: string): Promise<User | null>;
findUserById(id: string): Promise<User | null>
revokeToken(id: string, token: string): Promise<User | null>;
revokeToken(id: string, token: string): Promise<User | null>
}