mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 09:51:13 -05:00
feat: Create edge function to cancel email changes
This commit is contained in:
@@ -148,12 +148,16 @@ export function AccountProfileTab() {
|
|||||||
|
|
||||||
setCancellingEmail(true);
|
setCancellingEmail(true);
|
||||||
try {
|
try {
|
||||||
// Reset email to current email (effectively cancels the pending change)
|
// Call the edge function to cancel the email change with admin privileges
|
||||||
const { error: updateError } = await supabase.auth.updateUser({
|
const { data, error } = await supabase.functions.invoke('cancel-email-change', {
|
||||||
email: user.email
|
method: 'POST',
|
||||||
});
|
});
|
||||||
|
|
||||||
if (updateError) throw updateError;
|
if (error) throw error;
|
||||||
|
|
||||||
|
if (!data?.success) {
|
||||||
|
throw new Error(data?.error || 'Failed to cancel email change');
|
||||||
|
}
|
||||||
|
|
||||||
// Update Novu subscriber back to current email
|
// Update Novu subscriber back to current email
|
||||||
if (notificationService.isEnabled()) {
|
if (notificationService.isEnabled()) {
|
||||||
@@ -164,18 +168,6 @@ export function AccountProfileTab() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log the cancellation in audit log
|
|
||||||
await supabase.from('admin_audit_log').insert({
|
|
||||||
admin_user_id: user.id,
|
|
||||||
target_user_id: user.id,
|
|
||||||
action: 'email_change_cancelled',
|
|
||||||
details: {
|
|
||||||
cancelled_email: pendingEmail,
|
|
||||||
current_email: user.email,
|
|
||||||
timestamp: new Date().toISOString()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
sonnerToast.success('Email change cancelled', {
|
sonnerToast.success('Email change cancelled', {
|
||||||
description: 'Your email change request has been cancelled.'
|
description: 'Your email change request has been cancelled.'
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -22,4 +22,7 @@ verify_jwt = false
|
|||||||
verify_jwt = true
|
verify_jwt = true
|
||||||
|
|
||||||
[functions.send-escalation-notification]
|
[functions.send-escalation-notification]
|
||||||
|
verify_jwt = true
|
||||||
|
|
||||||
|
[functions.cancel-email-change]
|
||||||
verify_jwt = true
|
verify_jwt = true
|
||||||
103
supabase/functions/cancel-email-change/index.ts
Normal file
103
supabase/functions/cancel-email-change/index.ts
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.57.4';
|
||||||
|
|
||||||
|
const corsHeaders = {
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
||||||
|
};
|
||||||
|
|
||||||
|
Deno.serve(async (req) => {
|
||||||
|
// Handle CORS preflight requests
|
||||||
|
if (req.method === 'OPTIONS') {
|
||||||
|
return new Response(null, { headers: corsHeaders });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Create admin client with service role key
|
||||||
|
const supabaseAdmin = createClient(
|
||||||
|
Deno.env.get('SUPABASE_URL') ?? '',
|
||||||
|
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? '',
|
||||||
|
{
|
||||||
|
auth: {
|
||||||
|
autoRefreshToken: false,
|
||||||
|
persistSession: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Get the user from the authorization header
|
||||||
|
const authHeader = req.headers.get('Authorization');
|
||||||
|
if (!authHeader) {
|
||||||
|
throw new Error('No authorization header');
|
||||||
|
}
|
||||||
|
|
||||||
|
const token = authHeader.replace('Bearer ', '');
|
||||||
|
const { data: { user }, error: userError } = await supabaseAdmin.auth.getUser(token);
|
||||||
|
|
||||||
|
if (userError || !user) {
|
||||||
|
throw new Error('Unauthorized');
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Cancelling email change for user ${user.id}`);
|
||||||
|
|
||||||
|
// Use admin client to update the user and clear email_change fields
|
||||||
|
const { data: updatedUser, error: updateError } = await supabaseAdmin.auth.admin.updateUserById(
|
||||||
|
user.id,
|
||||||
|
{
|
||||||
|
email_confirm_change: user.email, // Reset to current email
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (updateError) {
|
||||||
|
console.error('Error cancelling email change:', updateError);
|
||||||
|
throw updateError;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Successfully cancelled email change for user ${user.id}`);
|
||||||
|
|
||||||
|
// Log the cancellation in admin_audit_log
|
||||||
|
const { error: auditError } = await supabaseAdmin
|
||||||
|
.from('admin_audit_log')
|
||||||
|
.insert({
|
||||||
|
admin_user_id: user.id,
|
||||||
|
target_user_id: user.id,
|
||||||
|
action: 'email_change_cancelled',
|
||||||
|
details: {
|
||||||
|
cancelled_at: new Date().toISOString(),
|
||||||
|
current_email: user.email,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (auditError) {
|
||||||
|
console.error('Error logging audit:', auditError);
|
||||||
|
// Don't fail the request if audit logging fails
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
success: true,
|
||||||
|
message: 'Email change cancelled successfully',
|
||||||
|
user: {
|
||||||
|
id: updatedUser.user.id,
|
||||||
|
email: updatedUser.user.email,
|
||||||
|
new_email: updatedUser.user.new_email,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||||
|
status: 200,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error in cancel-email-change function:', error);
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
success: false,
|
||||||
|
error: error.message,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||||
|
status: 400,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user