diff --git a/app/api/email/route.ts b/app/api/email/route.ts index d4c260b092075c3ad842b6a6bbc7238af03dba88..9a5f682f787ce57b14a021b21b4528ba72973880 100644 --- a/app/api/email/route.ts +++ b/app/api/email/route.ts @@ -1,47 +1,37 @@ -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 diff --git a/components/user-auth-form.tsx b/components/user-auth-form.tsx index d0c75d1899b9d757c86a771ed18b58f5f75a3860..0326ca69caf4d992c6f6f4ec07e5f79b6987f42e 100644 --- a/components/user-auth-form.tsx +++ b/components/user-auth-form.tsx @@ -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.", diff --git a/lib/auth.ts b/lib/auth.ts index 683473402af3fa0e47b486061ac03827ad5f560b..b1696bb684645aba9781eba6b7888252c52827ce 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -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,