@@ -2,6 +2,19 @@ import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
22import { createClient } from "https://esm.sh/@supabase/supabase-js@2" ;
33import { deleteListingMedia } from "../_shared/storage-utils.ts" ;
44
5+ function jsonResponse ( body : Record < string , unknown > , status : number ) {
6+ return new Response ( JSON . stringify ( body ) , {
7+ headers : {
8+ "Content-Type" : "application/json" ,
9+ } ,
10+ status,
11+ } ) ;
12+ }
13+
14+ function getErrorMessage ( error : unknown ) {
15+ return error instanceof Error ? error . message : "Unexpected error" ;
16+ }
17+
518serve ( async ( req ) => {
619 const supabaseAdmin = createClient (
720 Deno . env . get ( "SUPABASE_URL" ) ?? "" ,
@@ -13,14 +26,34 @@ serve(async (req) => {
1326 } ,
1427 }
1528 ) ;
16- const { user_id } = await req . json ( ) ;
29+
1730 try {
31+ if ( req . method !== "POST" ) {
32+ return jsonResponse ( { error : "Method not allowed" } , 405 ) ;
33+ }
34+
35+ const authHeader = req . headers . get ( "Authorization" ) ;
36+ const accessToken = authHeader ?. replace ( "Bearer " , "" ) ;
37+
38+ if ( ! accessToken ) {
39+ return jsonResponse ( { error : "Missing access token" } , 401 ) ;
40+ }
41+
42+ const {
43+ data : { user } ,
44+ error : userError ,
45+ } = await supabaseAdmin . auth . getUser ( accessToken ) ;
46+
47+ if ( userError || ! user ) {
48+ return jsonResponse ( { error : "Invalid access token" } , 401 ) ;
49+ }
50+
1851 // Get the user's profile to find avatar
1952 const { data : profile , error : profileError } = await supabaseAdmin
2053 . from ( "profiles" )
2154 . select ( "avatar" )
22- . eq ( "id" , user_id )
23- . single ( ) ;
55+ . eq ( "id" , user . id )
56+ . maybeSingle ( ) ;
2457 if ( profileError ) {
2558 console . error ( "Profile fetch error:" , profileError ) ;
2659 throw profileError ;
@@ -40,7 +73,7 @@ serve(async (req) => {
4073 const { data : listings , error : listingsError } = await supabaseAdmin
4174 . from ( "listings" )
4275 . select ( "slug" )
43- . eq ( "owner_id" , user_id ) ;
76+ . eq ( "owner_id" , user . id ) ;
4477 if ( listingsError ) {
4578 console . error ( "Listings fetch error:" , listingsError ) ;
4679 throw listingsError ;
@@ -59,35 +92,15 @@ serve(async (req) => {
5992 }
6093 // Delete auth user (cascade will handle the rest)
6194 const { error : deleteUserError } =
62- await supabaseAdmin . auth . admin . deleteUser ( user_id ) ;
95+ await supabaseAdmin . auth . admin . deleteUser ( user . id ) ;
6396 if ( deleteUserError ) {
6497 console . error ( "Auth user deletion error:" , deleteUserError ) ;
6598 throw deleteUserError ;
6699 }
67100 console . log ( "Deleted auth user" ) ;
68- return new Response (
69- JSON . stringify ( {
70- success : true ,
71- } ) ,
72- {
73- headers : {
74- "Content-Type" : "application/json" ,
75- } ,
76- status : 200 ,
77- }
78- ) ;
101+ return jsonResponse ( { success : true } , 200 ) ;
79102 } catch ( error ) {
80103 console . error ( "Final error:" , error ) ;
81- return new Response (
82- JSON . stringify ( {
83- error : error . message ,
84- } ) ,
85- {
86- headers : {
87- "Content-Type" : "application/json" ,
88- } ,
89- status : 400 ,
90- }
91- ) ;
104+ return jsonResponse ( { error : getErrorMessage ( error ) } , 400 ) ;
92105 }
93106} ) ;
0 commit comments