Skip to content

Commit a14cb75

Browse files
committed
fix: update email sender to use environment variable and improve error handling in authFetch
1 parent b5df3ff commit a14cb75

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

Backend-Express/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ app.post("/api/send-blog-email", verifyAuth, async (req, res) => {
948948

949949
// Send email with BCC
950950
await transporter.sendMail({
951-
from: '"CodeSapiens Blog" <suryasunrise261@gmail.com>',
951+
from: `"CodeSapiens Blog" <${process.env.EMAIL_USER}>`,
952952
to: "suryasunrise261@gmail.com", // Send to self/admin as primary recipient
953953
bcc: emails, // All recipients in BCC
954954
subject: `📚 New Blog: ${blog.title}`,
@@ -999,7 +999,7 @@ app.post("/api/send-blog-email-all", verifyAuth, async (req, res) => {
999999

10001000
// Send email with BCC
10011001
await transporter.sendMail({
1002-
from: '"CodeSapiens Blog" <suryasunrise261@gmail.com>',
1002+
from: `"CodeSapiens Blog" <${process.env.EMAIL_USER}>`,
10031003
to: "suryasunrise261@gmail.com", // Send to self/admin as primary recipient
10041004
bcc: emails, // All recipients in BCC
10051005
subject: `📚 New Blog: ${blog.title}`,
@@ -1030,7 +1030,7 @@ app.post("/api/test-email", verifyAuth, async (req, res) => {
10301030
}
10311031

10321032
await transporter.sendMail({
1033-
from: '"CodeSapiens" <suryasunrise261@gmail.com>',
1033+
from: `"CodeSapiens" <${process.env.EMAIL_USER}>`,
10341034
to: email,
10351035
subject: "Test Email from CodeSapiens",
10361036
html: `
@@ -1306,7 +1306,7 @@ const generateApprovalEmailHTML = (data) => {
13061306
};
13071307

13081308
app.post(
1309-
"/send-approval-email",
1309+
"/api/send-approval-email",
13101310
verifyAuth,
13111311
[
13121312
body('email').isEmail().withMessage('Valid email is required'),
@@ -1334,7 +1334,7 @@ app.post(
13341334
});
13351335

13361336
await transporter.sendMail({
1337-
from: '"CodeSapiens Meetups" <suryasunrise261@gmail.com>',
1337+
from: `"CodeSapiens Meetups" <${process.env.EMAIL_USER}>`,
13381338
to: email,
13391339
subject: `✅ Registration Approved: ${meetupTitle}`,
13401340
html: htmlContent,
@@ -1440,7 +1440,7 @@ const generateRejectionEmailHTML = (data) => {
14401440
};
14411441

14421442
app.post(
1443-
"/send-rejection-email",
1443+
"/api/send-rejection-email",
14441444
verifyAuth,
14451445
[
14461446
body('email').isEmail().withMessage('Valid email is required'),
@@ -1466,7 +1466,7 @@ app.post(
14661466
});
14671467

14681468
await transporter.sendMail({
1469-
from: '"CodeSapiens Meetups" <suryasunrise261@gmail.com>',
1469+
from: `"CodeSapiens Meetups" <${process.env.EMAIL_USER}>`,
14701470
to: email,
14711471
subject: `Registration Update: ${meetupTitle}`,
14721472
html: htmlContent,

src/lib/authFetch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,10 @@ export async function authFetch(input, init = {}) {
3636
// You could also redirect to /login here
3737
}
3838

39+
if (!resp.ok) {
40+
const errorData = await resp.json().catch(() => ({}));
41+
throw new Error(errorData.error || errorData.message || `HTTP error! status: ${resp.status}`);
42+
}
43+
3944
return resp;
4045
}

src/pages/admin/AdminMeetupRegistrations.jsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import { BACKEND_URL } from "../../config";
99

1010
// Helper function to format date without timezone conversion
1111
const formatMeetupDate = (dateTimeString) => {
12-
console.log("formatMeetupDate Input:", dateTimeString);
1312
if (!dateTimeString) return "TBA";
1413
// Take only the date-time part (first 19 chars: YYYY-MM-DDTHH:MM:SS)
1514
const raw = dateTimeString.slice(0, 19);
16-
console.log("formatMeetupDate Sliced:", raw);
1715

1816
const [datePart, timePart] = raw.split("T");
1917
const [year, month, day] = datePart.split("-");
@@ -26,7 +24,7 @@ const formatMeetupDate = (dateTimeString) => {
2624
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
2725
const monthName = months[parseInt(month, 10) - 1];
2826

29-
return `${monthName} ${parseInt(day, 10)}, ${year}, ${hour12}:${minute} ${ampm} (Event Time)`;
27+
return `${monthName} ${parseInt(day, 10)}, ${year}, ${hour12}:${minute} ${ampm}`;
3028
};
3129

3230
const AdminMeetupRegistrations = () => {
@@ -129,7 +127,7 @@ const AdminMeetupRegistrations = () => {
129127

130128
if (email && meetup) {
131129
try {
132-
await authFetch(`${BACKEND_URL}/send-approval-email`, {
130+
await authFetch(`${BACKEND_URL}/api/send-approval-email`, {
133131
method: "POST",
134132
headers: { "Content-Type": "application/json" },
135133
body: JSON.stringify({
@@ -144,7 +142,7 @@ const AdminMeetupRegistrations = () => {
144142
toast.success(`Approved & email sent to ${email}`);
145143
} catch (emailErr) {
146144
console.error("Email send error:", emailErr);
147-
toast.success("Approved! (Email failed to send)");
145+
toast.error(`Approved, but email failed: ${emailErr.message}`);
148146
}
149147
} else {
150148
toast.success("Registration approved!");
@@ -179,7 +177,7 @@ const AdminMeetupRegistrations = () => {
179177

180178
if (email && meetup) {
181179
try {
182-
await authFetch(`${BACKEND_URL}/send-rejection-email`, {
180+
await authFetch(`${BACKEND_URL}/api/send-rejection-email`, {
183181
method: "POST",
184182
headers: { "Content-Type": "application/json" },
185183
body: JSON.stringify({
@@ -193,7 +191,7 @@ const AdminMeetupRegistrations = () => {
193191
toast.success(`Rejected & email sent to ${email}`);
194192
} catch (emailErr) {
195193
console.error("Email send error:", emailErr);
196-
toast.success("Rejected! (Email failed to send)");
194+
toast.error(`Rejected, but email failed: ${emailErr.message}`);
197195
}
198196
} else {
199197
toast.success("Registration rejected");

0 commit comments

Comments
 (0)