From aafd0c7bf92b197bee32fb9a5d07496c875647fc Mon Sep 17 00:00:00 2001
From: Caner <s86215@bht-berlin.de>
Date: Sat, 1 Jul 2023 14:30:01 +0200
Subject: [PATCH] funktioniert noch nicht

---
 app/api/email/route.ts        | 28 ++++++++----------
 components/user-auth-form.tsx | 56 +++++++++++++++++++----------------
 2 files changed, 42 insertions(+), 42 deletions(-)

diff --git a/app/api/email/route.ts b/app/api/email/route.ts
index db67cc8..8ab1a1a 100644
--- a/app/api/email/route.ts
+++ b/app/api/email/route.ts
@@ -1,11 +1,14 @@
 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
diff --git a/components/user-auth-form.tsx b/components/user-auth-form.tsx
index 4101b47..dd9ce46 100644
--- a/components/user-auth-form.tsx
+++ b/components/user-auth-form.tsx
@@ -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")
 
-- 
GitLab