Fix password state sync for OAuth users

This commit is contained in:
gpt-engineer-app[bot]
2025-10-14 14:56:02 +00:00
parent 8594291ad2
commit 15a4ca5e76
2 changed files with 67 additions and 17 deletions

View File

@@ -160,6 +160,29 @@ export async function connectIdentity(
}
}
/**
* Wait for email provider to be created after password addition
* Supabase takes time to create the email identity, so we poll with retries
*/
async function waitForEmailProvider(maxRetries = 4): Promise<boolean> {
const delays = [500, 1000, 1500, 2000]; // Exponential backoff
for (let i = 0; i < maxRetries; i++) {
const identities = await getUserIdentities();
const hasEmail = identities.some(id => id.provider === 'email');
if (hasEmail) {
return true;
}
if (i < maxRetries - 1) {
await new Promise(resolve => setTimeout(resolve, delays[i]));
}
}
return false;
}
/**
* Add password authentication to an OAuth-only account
*/
@@ -180,6 +203,19 @@ export async function addPasswordToAccount(
if (error) throw error;
// Force session refresh to sync identity state
const { error: refreshError } = await supabase.auth.refreshSession();
if (refreshError) {
console.warn('[IdentityService] Session refresh failed:', refreshError);
}
// Wait for email provider to be created
const emailCreated = await waitForEmailProvider();
if (!emailCreated) {
console.warn('[IdentityService] Email provider not found after password addition');
}
// Log audit event
const { data: { user } } = await supabase.auth.getUser();
if (user) {