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

MAN KANN EINE EMAIL SENDEN

parent 9536c40d
No related branches found
No related tags found
1 merge request!45Email verify
Pipeline #39111 passed
import type { NextApiRequest, NextApiResponse } from 'next';
import nodemailer from 'nodemailer';
// Nodemailer docs: // https://nodemailer.com/about/
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") {
return res.status(405).json({ error: "Method Not Allowed" });
}
// https://nodemailer.com/smtp/
import nodemailer from "nodemailer";
import { NextResponse } from "next/server";
export async function POST(req: Request) {
const { email, subject, html } = await req.json();
const transporter = nodemailer.createTransport({
host: "smtp.gameunity.tgos@gmail.com",
port: 587,
service: 'gmail',
host: 'smtp.gmail.com',
auth: {
user: process.env.NODEMAIL_MAIL,
pass: process.env.NODEMAIL_PW,
},
});
const { subject, email, html } = req.body;
if (!subject || !email || !html) {
return res
.status(400)
.json({ message: 'Please fill out the necessary fields' });
}
// https://nodemailer.com/message/#common-fields
const mailData = {
from: process.env.NODEMAIL_MAIL,
from: email,
to: email,
subject: `${subject}`,
text: `${subject} | Sent from: ${process.env.NODEMAIL_MAIL}`,
text: `${subject} | Sent from: ${email}`,
html: `${html}`,
};
let emailRes;
try {
const info = await transporter.sendMail(mailData);
res.status(200).json({ message: 'Message sent!', info });
emailRes = await transporter.sendMail(mailData);
console.log("Message sent", emailRes.messageId);
} catch (err) {
res.status(500).json({ error: err || 'Something went wrong Message could not be send' });
console.log(err);
console.error("Error email could not be send");
}
return;
console.log(emailRes?.messageId)
return NextResponse.json({ success: true, messageId: emailRes?.messageId });
}
\ No newline at end of file
......@@ -36,34 +36,35 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
const router = useRouter();
const searchParams = useSearchParams()
// async function sendVerificationEmail(email: string) {
// try {
// const res = await fetch('/api/email', {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({
// email,
// subject: 'Email Verification',
// html: '<h1>Please verify your email address.</h1>',
// }),
// });
//muss noch exportiert werden
async function sendVerificationEmail(email: string) {
try {
const res = await fetch('/api/email', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
subject: 'Email Verification',
html: 'Please verify your email address.',
}),
});
// const body = await res.json();
const body = await res.json();
// if (res.ok) {
// alert(`${body.message} 🚀`);
// }
if (res.ok) {
alert(`${body.message} 🚀`);
}
// if (res.status === 400) {
// alert(`${body.message} 😢`);
// }
// } catch (err) {
// console.log('Something went wrong: ', err);
// }
if (res.status === 400) {
alert(`${body.message} 😢`);
}
} catch (err) {
console.log('Something went wrong: ', err);
}
// }
}
async function onSubmit(data: FormData) {
setIsLoading(true)
......@@ -93,7 +94,6 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
description: "Your sign up request failed. Please try again.",
})
}
// await sendVerificationEmail(data.email!)
}
const signInResult = await signIn("credentials", {
......@@ -129,6 +129,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
router.push("/home")
if (type === "signup") {
await sendVerificationEmail(data.email!)
return toast({
title: "Congratulations!",
description: "Your account has been created. You will be redirected shortly.",
......
......@@ -55,10 +55,6 @@ export const authOptions: NextAuthOptions = {
throw new Error('invalid password')
}
if(!user.emailVerified){
throw new Error('email not verifyed')
}
return {
id: user.id,
username: user.username,
......
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