Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Move CORS preflight handling in a dedicated middleware.

Matthieu Hochlandera6aa070c3f2c
diff --git a/supabase/functions/_shared/cors.ts b/supabase/functions/_shared/cors.ts
new file mode 100644
index 00000000..44e91c72
--- /dev/null
+++ b/supabase/functions/_shared/cors.ts
@@ -0,0 +1,20 @@
+export const corsHeaders = {
+ "Access-Control-Allow-Origin": "*",
+ "Access-Control-Allow-Headers":
+ "authorization, x-client-info, apikey, content-type",
+ "Access-Control-Allow-Methods": "POST, PATCH, DELETE",
+};
+
+/**
+ * Handle OPTIONS requests for CORS preflight.
+ */
+export function OptionsMiddleware(
+ req: Request,
+ next: (req: Request) => Promise<Response>,
+) {
+ if (req.method === "OPTIONS") {
+ return new Response(null, { status: 204, headers: corsHeaders });
+ }
+
+ return next(req);
+}
diff --git a/supabase/functions/_shared/utils.ts b/supabase/functions/_shared/utils.ts
index 2195aae1..d4399cf2 100644
--- a/supabase/functions/_shared/utils.ts
+++ b/supabase/functions/_shared/utils.ts
@@ -1,9 +1,4 @@
-export const corsHeaders = {
- "Access-Control-Allow-Origin": "*",
- "Access-Control-Allow-Headers":
- "authorization, x-client-info, apikey, content-type",
- "Access-Control-Allow-Methods": "POST, PATCH, DELETE",
-};
+import { corsHeaders } from "./cors.ts";
export function createErrorResponse(status: number, message: string) {
return new Response(JSON.stringify({ status, message }), {
diff --git a/supabase/functions/merge_contacts/index.ts b/supabase/functions/merge_contacts/index.ts
index 7099aac1..bd3d28e7 100644
--- a/supabase/functions/merge_contacts/index.ts
+++ b/supabase/functions/merge_contacts/index.ts
@@ -1,7 +1,8 @@
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
import { sql, type Selectable } from "https://esm.sh/[email protected]";
import { db, type ContactsTable, CompiledQuery } from "../_shared/db.ts";
-import { corsHeaders, createErrorResponse } from "../_shared/utils.ts";
+import { corsHeaders, OptionsMiddleware } from "../_shared/cors.ts";
+import { createErrorResponse } from "../_shared/utils.ts";
import { AuthMiddleware, UserMiddleware } from "../_shared/authentication.ts";
type Contact = Selectable<ContactsTable>;
@@ -158,42 +159,36 @@ async function mergeContacts(
}
Deno.serve(async (req: Request) =>
- AuthMiddleware(req, async (req) =>
- UserMiddleware(req, async (req, user) => {
- // Handle CORS preflight
- if (req.method === "OPTIONS") {
- return new Response(null, {
- status: 204,
- headers: corsHeaders,
- });
- }
-
- // Handle POST request
- if (req.method === "POST") {
- try {
- const { loserId, winnerId } = await req.json();
-
- if (!loserId || !winnerId) {
- return createErrorResponse(400, "Missing loserId or winnerId");
+ OptionsMiddleware(req, async (req) =>
+ AuthMiddleware(req, async (req) =>
+ UserMiddleware(req, async (req, user) => {
+ // Handle POST request
+ if (req.method === "POST") {
+ try {
+ const { loserId, winnerId } = await req.json();
+
+ if (!loserId || !winnerId) {
+ return createErrorResponse(400, "Missing loserId or winnerId");
+ }
+
+ const result = await mergeContacts(loserId, winnerId, user.id);
+
+ return new Response(JSON.stringify(result), {
+ headers: { "Content-Type": "application/json", ...corsHeaders },
+ });
+ } catch (error) {
+ console.error("Merge failed:", error);
+ return createErrorResponse(
+ 500,
+ `Failed to merge contacts: ${
+ error instanceof Error ? error.message : "Unknown error"
+ }`,
+ );
}
-
- const result = await mergeContacts(loserId, winnerId, user.id);
-
- return new Response(JSON.stringify(result), {
- headers: { "Content-Type": "application/json", ...corsHeaders },
- });
- } catch (error) {
- console.error("Merge failed:", error);
- return createErrorResponse(
- 500,
- `Failed to merge contacts: ${
- error instanceof Error ? error.message : "Unknown error"
- }`,
- );
}
- }
- return createErrorResponse(405, "Method Not Allowed");
- }),
+ return createErrorResponse(405, "Method Not Allowed");
+ }),
+ ),
),
);
diff --git a/supabase/functions/update_password/index.ts b/supabase/functions/update_password/index.ts
index 059460f2..c43b26e6 100644
--- a/supabase/functions/update_password/index.ts
+++ b/supabase/functions/update_password/index.ts
@@ -1,5 +1,6 @@
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
-import { corsHeaders, createErrorResponse } from "../_shared/utils.ts";
+import { corsHeaders, OptionsMiddleware } from "../_shared/cors.ts";
+import { createErrorResponse } from "../_shared/utils.ts";
import { supabaseAdmin } from "../_shared/supabaseAdmin.ts";
import { AuthMiddleware, UserMiddleware } from "../_shared/authentication.ts";
@@ -23,20 +24,15 @@ async function updatePassword(user: any) {
}
Deno.serve(async (req: Request) =>
- AuthMiddleware(req, async (req) =>
- UserMiddleware(req, async (req, user) => {
- if (req.method === "OPTIONS") {
- return new Response(null, {
- status: 204,
- headers: corsHeaders,
- });
- }
+ OptionsMiddleware(req, async (req) =>
+ AuthMiddleware(req, async (req) =>
+ UserMiddleware(req, async (req, user) => {
+ if (req.method === "PATCH") {
+ return updatePassword(user);
+ }
- if (req.method === "PATCH") {
- return updatePassword(user);
- }
-
- return createErrorResponse(405, "Method Not Allowed");
- }),
+ return createErrorResponse(405, "Method Not Allowed");
+ }),
+ ),
),
);
diff --git a/supabase/functions/users/index.ts b/supabase/functions/users/index.ts
index 97611b8d..59693eb6 100644
--- a/supabase/functions/users/index.ts
+++ b/supabase/functions/users/index.ts
@@ -1,6 +1,7 @@
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
import { supabaseAdmin } from "../_shared/supabaseAdmin.ts";
-import { corsHeaders, createErrorResponse } from "../_shared/utils.ts";
+import { corsHeaders, OptionsMiddleware } from "../_shared/cors.ts";
+import { createErrorResponse } from "../_shared/utils.ts";
import { AuthMiddleware, UserMiddleware } from "../_shared/authentication.ts";
import { getUserSale } from "../_shared/getUserSale.ts";
@@ -169,29 +170,24 @@ async function patchUser(req: Request, currentUserSale: any) {
}
Deno.serve(async (req: Request) =>
- AuthMiddleware(req, async (req) =>
- UserMiddleware(req, async (req, user) => {
- if (req.method === "OPTIONS") {
- return new Response(null, {
- status: 204,
- headers: corsHeaders,
- });
- }
-
- const currentUserSale = await getUserSale(user);
- if (!currentUserSale) {
- return createErrorResponse(401, "Unauthorized");
- }
-
- if (req.method === "POST") {
- return inviteUser(req, currentUserSale);
- }
-
- if (req.method === "PATCH") {
- return patchUser(req, currentUserSale);
- }
-
- return createErrorResponse(405, "Method Not Allowed");
- }),
+ OptionsMiddleware(req, async (req) =>
+ AuthMiddleware(req, async (req) =>
+ UserMiddleware(req, async (req, user) => {
+ const currentUserSale = await getUserSale(user);
+ if (!currentUserSale) {
+ return createErrorResponse(401, "Unauthorized");
+ }
+
+ if (req.method === "POST") {
+ return inviteUser(req, currentUserSale);
+ }
+
+ if (req.method === "PATCH") {
+ return patchUser(req, currentUserSale);
+ }
+
+ return createErrorResponse(405, "Method Not Allowed");
+ }),
+ ),
),
);