diff --git a/supabase/functions/cancel-email-change/index.ts b/supabase/functions/cancel-email-change/index.ts index acfdba11..d18c06ed 100644 --- a/supabase/functions/cancel-email-change/index.ts +++ b/supabase/functions/cancel-email-change/index.ts @@ -37,13 +37,18 @@ Deno.serve(async (req) => { throw new Error('Unauthorized'); } - console.log(`Cancelling email change for user ${user.id}`); + console.log(`Cancelling email change for user ${user.id}`, { + currentEmail: user.email, + newEmail: user.new_email + }); - // Use admin client to update the user and clear email_change fields + // Use admin client to force reset email to current value + // This clears any pending email changes (new_email field) const { data: updatedUser, error: updateError } = await supabaseAdmin.auth.admin.updateUserById( user.id, { - email_confirm_change: user.email, // Reset to current email + email: user.email, + email_confirm: true, // Skip sending confirmation email since we're "changing" to same email } ); @@ -52,7 +57,10 @@ Deno.serve(async (req) => { throw updateError; } - console.log(`Successfully cancelled email change for user ${user.id}`); + console.log(`Successfully cancelled email change for user ${user.id}`, { + resultEmail: updatedUser.user.email, + resultNewEmail: updatedUser.user.new_email + }); // Log the cancellation in admin_audit_log const { error: auditError } = await supabaseAdmin