(WIP) Added pw reset token row, added checks to reset function
This commit is contained in:
@ -1,8 +1,9 @@
|
||||
import bcrypt from 'bcryptjs'
|
||||
import UserRepository from '../repositories/userRepository'
|
||||
import prisma from '../utilities/prisma'
|
||||
import { User } from '@prisma/client'
|
||||
import { User, PasswordResetToken } from '@prisma/client'
|
||||
import config from '../utilities/config'
|
||||
import NodeMailer from 'nodemailer'
|
||||
|
||||
/**
|
||||
* User service
|
||||
@ -59,10 +60,27 @@ class UserService {
|
||||
* Reset password
|
||||
* @param email
|
||||
*/
|
||||
async resetPassword(email: string): Promise<boolean | User> {
|
||||
const nodemailer = require("nodemailer");
|
||||
async resetPassword(email: string): Promise<boolean | undefined> {
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
const user = await UserRepository.getByEmail(email)
|
||||
if ( !user ) return
|
||||
const token = await bcrypt.genSalt(10)
|
||||
|
||||
//Check if password reset has been requested recently
|
||||
if (await prisma.passwordResetToken.findFirst({
|
||||
where: {
|
||||
userId: user.id
|
||||
},
|
||||
})) return
|
||||
|
||||
prisma.passwordResetToken.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
token: token,
|
||||
}
|
||||
});
|
||||
|
||||
const transporter = NodeMailer.createTransport({
|
||||
host: config.SMTP_HOST,
|
||||
port: config.SMTP_PORT,
|
||||
secure: false,
|
||||
@ -76,12 +94,15 @@ class UserService {
|
||||
from: config.SMTP_USER,
|
||||
to: email,
|
||||
subject: "Reset your password",
|
||||
text: "A password reset has been requested, reset your password here: ", // Plain text body
|
||||
html: "<p>A password reset has been requested, reset your password here: </p>", // Html body
|
||||
text: "A password reset has been requested, reset your password here: " + config.CLIENT_URL + "/" + token, // Plain text body
|
||||
html: "<p>A password reset has been requested, reset your password here: " + config.CLIENT_URL + "/" + token + "</p>", // Html body
|
||||
});
|
||||
console.log("Message sent: %s", info.messageId);
|
||||
|
||||
return info.messageId
|
||||
|
||||
if (info) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user