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

funktioniert noch nicht

parent 30292c28
No related branches found
No related tags found
1 merge request!45Email verify
Pipeline #38985 failed
import type { NextApiRequest, NextApiResponse } from 'next';
import * as nodemailer from 'nodemailer';
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/
const transporter = nodemailer.createTransport({
service: "gmail",
......@@ -26,26 +29,19 @@ export default async function handler(
// https://nodemailer.com/message/#common-fields
const mailData = {
from: email,
from: process.env.NODEMAIL_MAIL,
to: email,
subject: `${subject}`,
text: `${subject} | Sent from: ${email}`,
text: `${subject} | Sent from: ${process.env.NODEMAIL_MAIL}`,
html: `${html}`,
};
await new Promise((resolve, reject) => {
transporter.sendMail(mailData, (err: Error | null, info) => {
if (err) {
reject(err);
return res
.status(500)
.json({ error: err.message || 'Something went wrong' });
} else {
resolve(info.accepted);
res.status(200).json({ message: 'Message sent!' });
}
});
});
try {
const info = await transporter.sendMail(mailData);
res.status(200).json({ message: 'Message sent!', info });
} catch (err) {
res.status(500).json({ error: err || 'Something went wrong Message could not be send' });
}
return;
}
\ No newline at end of file
......@@ -36,6 +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',
text: 'Please verify your email address.',
}),
});
const body = await res.json();
if (res.ok) {
alert(`${body.message} 🚀`);
}
if (res.status === 400) {
alert(`${body.message} 😢`);
}
} catch (err) {
console.log('Something went wrong: ', err);
}
}
async function onSubmit(data: FormData) {
setIsLoading(true)
......@@ -95,32 +124,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
action: <ToastAction altText="Try again">Try again</ToastAction>,
})
}
try {
const res = await fetch('/api/email', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(
subject: "Test",
email: data.email,
),
});
const body = await res.json();
if (res.ok) {
alert(`${body.message} 🚀`);
}
if (res.status === 400) {
alert(`${body.message} 😢`);
}
} catch (err) {
console.log('Something went wrong: ', err);
}
await sendVerificationEmail(data.email!)
router.push("/home")
......
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