Reputation: 3
I'm currently working on a Flutter app that integrates with Supabase.I'm facing a FormatException with the following error message: "Unexpected character (at character 1) ." when trying to delete a user using Supabase Edge function.
The error occurs when I try to delete a user. This is my code snippet
Future<void> handlingUserDeletion() async {
try {
await supabase.functions.invoke(
"delete-user",
headers: {
'Authorization': 'Bearer ${supabase.auth.currentSession?.accessToken}'
},
);
if (mounted) {
supabase.auth.signOut();
}
} on PostgrestException catch (error) {
debugPrint(error.message);
} catch (e) {
debugPrint(e.toString());
}
}
This is my delete-user supabase edge function, deploy to supabase using Supabase CLI by following this YouTube tutorial
import { serve } from 'https://deno.land/[email protected]/http/server.ts';
import { createClient } from 'https://esm.sh/@supabase/[email protected]';
import { corsHeaders } from '../_shared/cors.ts';
console.log("Delete user account function");
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, true);
// Log deletion error so we can debug. Delete if not required!
// console.log(deletion_error);
if (deletion_error) throw 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,
})
}
});
This is my cors.ts file
export const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
}
Version Flutter 3.10.6 supabase_flutter: ^1.8.1
I've already tried diggin the supabase documentation, browse through Q&A on Stack Overflow, and Github issue, but doesn't match to what I'm looking for.
Can anyone help me understand why I'm getting this FormatException and suggest possible solutions? Any insights or guidance would be greatly appreciated.
Upvotes: 0
Views: 203
Reputation: 18690
You don't need to pass the auth headers, as invoke()
will automatically do that for you.
await supabase.functions.invoke("delete-user");
Also I would double check that you are calling the correct function name. If this is your first time calling a Supabase edge function, probably start from creating a simple function that logs something in the console, and verify that you can indeed call a function from your Flutter application.
Upvotes: 0