Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import nodemailer from "nodemailer";
import { NextResponse } from "next/server";
import { db } from "@/lib/db";
import { randomUUID } from "crypto";
import getURL from "@/lib/utils";
export async function POST(req: Request) {
const { email} = await req.json();
const transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
auth: {
user: process.env.NODEMAIL_MAIL,
pass: process.env.NODEMAIL_PW,
},
});
const user = await db.user.findFirst({
where: {
email: email
}
});
const token = await db.activationToken.create({
data: {
token: `${randomUUID()}${randomUUID()}`.replace(/-/g, ''),
userId: user?.id!
},
});
const mailData = {
from: process.env.NODEMAIL_MAIL,
to: email,
subject: ` 'Password Reset for your GameUnity account`,
html: `Hello ${user?.name} you requeste a password reset. \nPlease follow the Link: ${getURL(`/api/verification/${token.token}`)} to change your password.`,
};
let emailRes;
try {
emailRes = await transporter.sendMail(mailData);
console.log("Message sent", emailRes.messageId);
} catch (err) {
console.log(err);
console.error("Error email could not be send");
}
console.log(emailRes?.messageId)
return NextResponse.json({ success: true, messageId: emailRes?.messageId });
}