Skip to content

Commit

Permalink
mailing fix
Browse files Browse the repository at this point in the history
  • Loading branch information
aludayalu committed Oct 17, 2023
1 parent 6fa44ff commit dc4449e
Showing 1 changed file with 16 additions and 59 deletions.
75 changes: 16 additions & 59 deletions app/api/user/verify/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NextRequest, NextResponse } from "next/server";
import jwt from "jsonwebtoken";
import nodemailer from "nodemailer";
import client from "@/util/data/Mongo";
import { redirect } from "next/navigation";

Expand Down Expand Up @@ -90,52 +89,8 @@ export async function POST(req: NextRequest) {
}); */

// Initialize nodemailer
const transporter = await nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: process.env.GMAIL_USER,
pass: process.env.GMAIL_PASS,
},
});

// verify connection configuration
await new Promise((resolve, reject) => {
transporter.verify(function (err, success) {
if (err) {
reject(err);
return new NextResponse(JSON.stringify(err));
} else {
resolve(success);
}
});
});

// Function to generate emailOptions
const emailOptions = (url: string, to: string, teacher: boolean) => {
return {
from: "Exun Clan <[email protected]>",
to: to,
subject: `Exun 2023 ${teacher ? "Teacher In-charge " : ""}Verification`,
html: `
<p>
Please click on the following link to verify your email for Exun 2023. <br><br>
<a href="${url}">
${url}
</a>
<br><br>
This is your unique discord ${process.env
.DISCORD_INVITE_LINK!} verification token: <b>${randomToken}</b>
<br>
Kindly share it with the participants of your school.
<br><br>
Regards, <br>
Exun Clan
</p>
`,
};
};

// Sign jwt token if email exists
if (email) {
Expand All @@ -151,20 +106,22 @@ export async function POST(req: NextRequest) {
);

const url = `${process.env.NEXT_PUBLIC_URL}/api/user/verify?token=${token}`;

await new Promise((resolve, reject) => {
transporter.sendMail(emailOptions(url, email, false), (err, info) => {
if (err) {
reject(err);
return new NextResponse(JSON.stringify(err));
}

resolve(info);

/* // For test account
console.log(nodemailer.getTestMessageUrl(_)); */
});
});
await fetch(`https://exun-mailer.vercel.app/email?password=${"Tejas%20yaha%20password%20nhi%20milega%20tujhe%20iska%20sorry"}&to=${encodeURIComponent(email)}&subject=${encodeURIComponent(`Exun 2023 ${false ? "Teacher In-charge " : ""}Verification`)}&html=${`
<p>
Please click on the following link to verify your email for Exun 2023. <br><br>
<a href="${url}">
${url}
</a>
<br><br>
This is your unique discord ${process.env
.DISCORD_INVITE_LINK!} verification token: <b>${randomToken}</b>
<br>
Kindly share it with the participants of your school.
<br><br>
Regards, <br>
Exun Clan
</p>
`}`)
}

// Sign jwt token if teacherEmail exists
Expand Down

0 comments on commit dc4449e

Please sign in to comment.