Skip to content

Commit

Permalink
created a function to send email to user
Browse files Browse the repository at this point in the history
  • Loading branch information
benlee04 committed Oct 22, 2024
1 parent b5d353f commit fc9ac3f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const AccountInfo = () => {
<Text className="text-lg font-semibold">Email:</Text>
<Text className="text-base">{psqlUser?.email}</Text>
</View>

<View>
<Text className="text-lg font-semibold">Email Verification:</Text>
<Text
Expand Down
40 changes: 36 additions & 4 deletions lifescape-server/src/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,37 @@ import { db } from "../utils/db.server";
import { getAuth } from "firebase-admin/auth";
import { auth } from "firebase-admin";

import nodemailer from 'nodemailer';

const router = express.Router();

// Create a Nodemailer transporter
const transporter = nodemailer.createTransport({
service: 'gmail', // or another email service
auth: {
user: '[email protected]', // your email
pass: 'your-email-password', // your email password or app-specific password
},
});

// Function to send email with the verification link
const sendEmail = (toEmail: string, verificationLink: string) => {
const mailOptions = {
from: '[email protected]',
to: toEmail,
subject: 'Verify your email for LifeScape',
text: `Please verify your email by clicking the following link: ${verificationLink}`,
html: `<p>Please verify your email by clicking <a href="${verificationLink}">here</a>.</p>`,
};

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Email sent: ' + info.response);
});
};

/** @api {post} /users/register Register User
* @apiName RegisterUser
* @apiGroup User
Expand Down Expand Up @@ -182,14 +211,17 @@ router.post("/verify-email/:userId", async (req, res) => {
return res.status(400).json({ error: "User does not have an email" });
}

const link = await getAuth().generateEmailVerificationLink(userRecord.email);
console.log(`Generated email verification link: ${link}`);
const verificationLink = await getAuth().generateEmailVerificationLink(userRecord.email);
console.log(`Generated email verification link: ${verificationLink}`);

// Send email with the verification link
sendEmail(userRecord.email, verificationLink);

// Optionally: you can send this link via email or return it to the client (for now we'll return it)
// Return response to the client
return res.status(200).json({
message: "Verification email sent",
success: true,
verificationLink: link, // You can send this link via an actual email sending service later
verificationLink: verificationLink, // You can still return the link for testing purposes
});

} catch (error) {
Expand Down

0 comments on commit fc9ac3f

Please sign in to comment.