Skip to content

Commit 87bf5dc

Browse files
committed
address edge function review feedback
1 parent eb8473a commit 87bf5dc

5 files changed

Lines changed: 50 additions & 57 deletions

File tree

src/app/actions.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@ export const deleteListingAction = async (
691691
export const deleteAccountAction = async () => {
692692
const t = await getTranslations("Errors");
693693
let redirectPath: string | null = null;
694+
let shouldSignOut = false;
694695

695696
const supabase = await createClient();
696697
const {
@@ -726,13 +727,16 @@ export const deleteAccountAction = async () => {
726727
console.error("Delete account failed:", data);
727728
redirectPath = `/profile?error=${encodeURIComponent(t("deleteAccountFailed"))}`;
728729
} else {
730+
shouldSignOut = true;
729731
redirectPath = `/sign-in?success=${encodeURIComponent(t("accountDeleted"))}`;
730732
}
731733
} catch (error) {
732734
console.error("Delete account error:", error);
733735
redirectPath = `/profile?error=${encodeURIComponent(t("deleteAccountFailed"))}`;
734736
} finally {
735-
await supabase.auth.signOut();
737+
if (shouldSignOut) {
738+
await supabase.auth.signOut();
739+
}
736740
if (redirectPath) {
737741
return redirect(redirectPath);
738742
}

supabase/config.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,18 @@ inspector_port = 8084
275275
enabled = true
276276
verify_jwt = false
277277

278+
[functions.delete-account]
279+
enabled = true
280+
verify_jwt = true
281+
282+
[functions.delete-listing]
283+
enabled = true
284+
verify_jwt = true
285+
286+
[functions.send-email-for-new-chat-message]
287+
enabled = true
288+
verify_jwt = true
289+
278290
[analytics]
279291
enabled = true
280292
port = 54337

supabase/functions/delete-account/index.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,6 @@ function getErrorMessage(error: unknown) {
1717
}
1818

1919
serve(async (req) => {
20-
const supabaseAdmin = createClient(
21-
Deno.env.get("SUPABASE_URL") ?? "",
22-
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ?? "",
23-
{
24-
auth: {
25-
autoRefreshToken: false,
26-
persistSession: false,
27-
},
28-
}
29-
);
30-
3120
try {
3221
if (req.method !== "POST") {
3322
return jsonResponse({ error: "Method not allowed" }, 405);
@@ -39,6 +28,21 @@ serve(async (req) => {
3928
return jsonResponse({ error: "Missing access token" }, 401);
4029
}
4130

31+
const supabaseUrl = Deno.env.get("SUPABASE_URL");
32+
const supabaseServiceRoleKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY");
33+
34+
if (!supabaseUrl || !supabaseServiceRoleKey) {
35+
console.error("delete-account is missing required Supabase env vars");
36+
return jsonResponse({ error: "Function misconfigured" }, 500);
37+
}
38+
39+
const supabaseAdmin = createClient(supabaseUrl, supabaseServiceRoleKey, {
40+
auth: {
41+
autoRefreshToken: false,
42+
persistSession: false,
43+
},
44+
});
45+
4246
const {
4347
data: { user },
4448
error: userError,

supabase/functions/delete-listing/index.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@ function getErrorMessage(error: unknown) {
1515
}
1616

1717
serve(async (req) => {
18-
const supabaseAdmin = createClient(
19-
Deno.env.get("SUPABASE_URL") ?? "",
20-
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ?? "",
21-
{
22-
auth: {
23-
autoRefreshToken: false,
24-
persistSession: false,
25-
},
26-
}
27-
);
28-
2918
try {
3019
if (req.method !== "POST") {
3120
return jsonResponse({ error: "Method not allowed" }, 405);
@@ -43,6 +32,21 @@ serve(async (req) => {
4332
return jsonResponse({ error: "Missing access token" }, 401);
4433
}
4534

35+
const supabaseUrl = Deno.env.get("SUPABASE_URL");
36+
const supabaseServiceRoleKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY");
37+
38+
if (!supabaseUrl || !supabaseServiceRoleKey) {
39+
console.error("delete-listing is missing required Supabase env vars");
40+
return jsonResponse({ error: "Function misconfigured" }, 500);
41+
}
42+
43+
const supabaseAdmin = createClient(supabaseUrl, supabaseServiceRoleKey, {
44+
auth: {
45+
autoRefreshToken: false,
46+
persistSession: false,
47+
},
48+
});
49+
4650
const {
4751
data: { user },
4852
error: userError,

supabase/functions/send-email-for-new-chat-message/index.ts

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ const handler = async (_request: Request): Promise<Response> => {
1616
// Prepare data
1717
const { record } = await _request.json();
1818
if (!record) throw new Error("No record provided");
19-
console.log("Record:", record);
2019

2120
const supabaseUrl = Deno.env.get("SUPABASE_URL");
22-
console.log("Supabase URL:", supabaseUrl);
2321

2422
const supabaseServiceKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY");
2523
if (
@@ -33,6 +31,7 @@ const handler = async (_request: Request): Promise<Response> => {
3331

3432
const supabase = createClient(supabaseUrl, supabaseServiceKey);
3533
const resend = new Resend(RESEND_API_KEY);
34+
console.log("Processing chat message email", { messageId: record.id });
3635

3736
const { data: messageData, error: messageError } = await supabase
3837
.from("chat_messages")
@@ -44,8 +43,6 @@ const handler = async (_request: Request): Promise<Response> => {
4443
throw new Error(`Failed to fetch message data: ${messageError?.message}`);
4544
}
4645

47-
console.log("Message Data:", messageData);
48-
4946
const { data: threadData, error: threadError } = await supabase
5047
.from("chat_threads")
5148
.select("id, listing_id, initiator_id, owner_id")
@@ -95,32 +92,21 @@ const handler = async (_request: Request): Promise<Response> => {
9592
const ownerProfile = profileById.get(threadData.owner_id);
9693

9794
const senderName = senderProfile?.first_name ?? "Someone";
98-
console.log("Sender name:", senderName);
9995

10096
const senderAvatar = senderProfile?.avatar;
101-
console.log("Sender avatar:", senderAvatar);
10297

10398
const listingAvatar = listingData.avatar;
104-
console.log("Listing avatar:", listingAvatar);
10599

106100
const listingSlug = listingData.slug ?? "";
107-
console.log("Listing slug:", listingSlug);
108101

109102
const listingName = listingData.name;
110-
console.log("Listing name:", listingName);
111103

112104
const listingType = listingData.type;
113-
console.log("Listing type:", listingType);
114105

115106
const listingAreaName = listingData.area_name ?? "";
116-
console.log("Listing area name:", listingAreaName);
117107

118108
const ownerHasMultipleNonResidentialListings =
119109
listingData.owner_has_multiple_non_residential_listings;
120-
console.log(
121-
"Owner has multiple non-residential listings:",
122-
ownerHasMultipleNonResidentialListings
123-
);
124110

125111
// Determine recipient_id (the user who isn't the sender)
126112
const recipientId =
@@ -152,7 +138,6 @@ const handler = async (_request: Request): Promise<Response> => {
152138
avatarMajorUrl = senderAvatar;
153139
avatarMajorBucket = "avatars";
154140
avatarMinorUrl = null; // No secondary avatar needed
155-
console.log("Recipient is owner. Showing sender avatar:", senderAvatar);
156141
} else {
157142
// If the recipient is the initiator (donor/guest)
158143
if (listingType === "residential") {
@@ -161,39 +146,23 @@ const handler = async (_request: Request): Promise<Response> => {
161146
avatarMajorUrl = senderAvatar;
162147
avatarMajorBucket = "avatars";
163148
avatarMinorUrl = null; // No secondary avatar needed
164-
console.log(
165-
"Recipient is initiator (residential). Showing sender avatar:",
166-
senderAvatar
167-
);
168149
} else {
169150
// For non-residential listings, show the listing's avatar as primary
170151
// and the sender's (host's) avatar as secondary.
171152
avatarMajorUrl = listingAvatar;
172153
avatarMajorBucket = "listing_avatars";
173154
avatarMinorUrl = senderAvatar;
174-
console.log(
175-
"Recipient is initiator (non-residential). Showing listing avatar:",
176-
listingAvatar,
177-
"and sender avatar:",
178-
senderAvatar
179-
);
180155
}
181156
}
182157

183-
console.log("Sender ID from chat_messages:", record.sender_id);
184-
console.log("Recipient ID from chat_threads:", recipientId);
185-
console.log("Message:", record.content);
186-
console.log("Recipient Role:", recipientRole);
187-
188158
// Do auth admin query to get recipient email (keeping this pattern for security)
189159
// TODO: Minify this query to just ask for the email address
190160
const { data: recipientData, error: recipientError } =
191161
await supabase.auth.admin.getUserById(recipientId);
192-
console.log("Recipient data:", recipientData);
193-
if (recipientError) console.log("Recipient error:", recipientError);
162+
if (recipientError)
163+
console.error("Recipient lookup error:", recipientError);
194164

195165
const recipientEmail = recipientData?.user?.email;
196-
console.log("Recipient Email:", recipientEmail);
197166

198167
if (!recipientEmail) {
199168
throw new Error(`No email found for recipient ${recipientId}`);

0 commit comments

Comments
 (0)