resend email in progress

This commit is contained in:
2023-06-14 16:13:22 +02:00
parent 6f2fab076d
commit 612b2326f9
17 changed files with 181 additions and 164 deletions

View File

@@ -21,11 +21,12 @@ export class UsersService {
async createUser(name: string, password: string, email: string): Promise<UserViewType | null> {
const passwordHash = await this._generateHash(password)
const verificationToken = uuidv4()
const newUser: CreateUserInput = {
name: name || email.split('@')[0],
email: email,
password: passwordHash,
verificationToken: uuidv4(),
verificationToken,
verificationTokenExpiry: addHours(new Date(), 24),
isEmailVerified: false,
}
@@ -33,23 +34,32 @@ export class UsersService {
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)
}
await this.sendConfirmationEmail({
email: createdUser.email,
name: createdUser.name,
verificationToken: verificationToken,
})
return {
id: createdUser.id,
name: createdUser.name,
email: createdUser.email,
}
}
async resendConfirmationEmail(userId: string) {
const user = await this.usersRepository.findUserById(userId, { verification: true })
if (!user) {
return null
}
if (user.isEmailVerified) {
return null
}
await this.sendConfirmationEmail({
email: user.email,
name: user.name,
verificationToken: user.verification.verificationToken,
})
return true
}
async deleteUserById(id: string): Promise<boolean> {
return await this.usersRepository.deleteUserById(id)
}
@@ -70,7 +80,27 @@ export class UsersService {
return null
}
}
private async sendConfirmationEmail({
email,
name,
verificationToken,
}: {
email: string
name: string
verificationToken: string
}) {
try {
await this.emailService.sendMail({
from: 'andrii <andrii@andrii.es>',
to: email,
text: 'hello and welcome, token is: ' + verificationToken,
html: `<b>Hello ${name}!</b><br/>Please confirm your email by clicking on the link below:<br/><a href="http://localhost:3000/confirm-email/${verificationToken}">Confirm email</a>`,
subject: 'E-mail confirmation ',
})
} catch (e) {
console.log(e)
}
}
private async _generateHash(password: string) {
return await bcrypt.hash(password, 10)
}
@@ -88,7 +118,7 @@ export interface IUsersRepository {
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>
}