mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 09:51:13 -05:00
Implement PostgreSQL function to cancel email change
This commit is contained in:
@@ -42,45 +42,14 @@ Deno.serve(async (req) => {
|
||||
newEmail: user.new_email
|
||||
});
|
||||
|
||||
// Direct database manipulation to clear email change fields
|
||||
// Use service role to directly query and update auth.users table
|
||||
const { error: sqlError } = await supabaseAdmin
|
||||
.from('auth.users')
|
||||
.update({
|
||||
email_change: null,
|
||||
email_change_sent_at: null,
|
||||
email_change_confirm_status: 0,
|
||||
email_change_token_current: null,
|
||||
email_change_token_new: null
|
||||
})
|
||||
.eq('id', user.id);
|
||||
// Call the database function to clear email change fields
|
||||
// This function has SECURITY DEFINER privileges to access auth.users
|
||||
const { data: cancelled, error: cancelError } = await supabaseAdmin
|
||||
.rpc('cancel_user_email_change', { _user_id: user.id });
|
||||
|
||||
if (sqlError) {
|
||||
console.error('Error clearing email change fields via SQL:', sqlError);
|
||||
|
||||
// Fallback: Try using the two-step email update trick
|
||||
console.log('Attempting fallback method with temporary email...');
|
||||
try {
|
||||
const tempEmail = `temp_${Date.now()}_${user.email}`;
|
||||
const { error: tempError } = await supabaseAdmin.auth.admin.updateUserById(
|
||||
user.id,
|
||||
{ email: tempEmail, email_confirm: true }
|
||||
);
|
||||
|
||||
if (tempError) throw tempError;
|
||||
|
||||
const { error: revertError } = await supabaseAdmin.auth.admin.updateUserById(
|
||||
user.id,
|
||||
{ email: user.email, email_confirm: true }
|
||||
);
|
||||
|
||||
if (revertError) throw revertError;
|
||||
|
||||
console.log('Fallback method succeeded');
|
||||
} catch (fallbackError) {
|
||||
console.error('Fallback method also failed:', fallbackError);
|
||||
throw new Error('Unable to cancel email change: ' + fallbackError.message);
|
||||
}
|
||||
if (cancelError || !cancelled) {
|
||||
console.error('Error cancelling email change:', cancelError);
|
||||
throw new Error('Unable to cancel email change: ' + (cancelError?.message || 'Unknown error'));
|
||||
}
|
||||
|
||||
// Verify the change was successful by getting the updated user
|
||||
|
||||
Reference in New Issue
Block a user