Hello, many of us are encountering an issue with edge functions on Supabase. The edge function allows you to delete a Supabase user from FlutterFlow with an API call.
I followed the No Code Academy video:
https://www.youtube.com/watch?v=PNBvc35CDAk
The method worked before, but the new comments all have the same issue: "No Users Found with JWT."
The problem comes from the supabase.auth.getUser() function, which returns null even though the user is authenticated, the JWT is valid, and is correctly retrieved from the API call headers.
I've been blocked by the App Store because the delete functionality isn't working. It's urgent, can you help me?
IdeaGarage, Serge Middendorf, Dimitar Klaturov your experience will be useful to us. Thank You
Edge function code:
import { serve } from 'https://deno.land/[email protected]/http/server.ts';
import { createClient } from 'https://esm.sh/@supabase/[email protected]';
console.log("Delete user account function");
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type'
};
serve(async (request) => {
// This is needed if you're planning to invoke your function from a browser.
if (request.method === 'OPTIONS') {
return new Response('ok', { headers: corsHeaders })
}
try {
//Create instance of SupabaseClient
const supabaseClient = createClient(
Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? '',
{ global: { headers: { Authorization: request.headers.get('Authorization')! } } }
);
// Create a user object which contains the data we need to identify the user.id
const {
data: { user },
} = await supabaseClient.auth.getUser()
// Throw an error if there are any issues with identifying the users from the token
if (!user) throw new Error('No user found for JWT!');
// Create supabaseAdmin client which specifically uses the Service Role
// Key in order to perform elavated administration actins
const supabaseAdmin = createClient(
Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
)
// Call the deleteUser method on the supabaseAdmin client and pass the user.id
const { data: deletion_data, error: deletion_error } = await supabaseAdmin.auth.admin.deleteUser(user.id)
// Log deletion error so we can debug. Delete if not required!
console.log(deletion_error);
// Return a response of the user which has been deleted
return new Response('User deleted: ' + JSON.stringify(deletion_data, null, 2), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 200,
});
} catch (error) {
// Return an error with the error message should it run in to any issues
return new Response(JSON.stringify({ error: error.message }), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 400,
})
}
});