Skip to content
Snippets Groups Projects
Commit 17b0344a authored by Caner's avatar Caner
Browse files

EmailVerification ohne Password reset

parent 64f2341a
No related branches found
No related tags found
1 merge request!45Email verify
Pipeline #39904 passed
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 });
}
\ No newline at end of file
......@@ -94,7 +94,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
}
if (signInResult.error === "Email is not verified") {
return toast({
title: "Please Verify your account.",
title: "Please verify your account.",
description: "We send you a email to verify your Account. Please check your Mailbox!🚀",
})
}
......
export async function sendPasswordResetEmail(email: string) {
try {
const res = await fetch('/api/passwordChange', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
}),
});
if (res.ok) {
alert(`PasswordChange Email was send 🚀`);
}
if (res.status === 400) {
alert(`Something went wrong! Password reset Email could not be send 😢`);
}
} catch (err) {
console.log('Something went wrong: ', err);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment