Refactor security functions

This commit is contained in:
gpt-engineer-app[bot]
2025-10-14 19:38:36 +00:00
parent 1554254c82
commit 95972a0b22
9 changed files with 638 additions and 89 deletions

View File

@@ -11,6 +11,7 @@ import type {
IdentitySafetyCheck,
IdentityOperationResult
} from '@/types/identity';
import { logger } from './logger';
/**
* Get all identities for the current user
@@ -23,7 +24,10 @@ export async function getUserIdentities(): Promise<UserIdentity[]> {
return (data?.identities || []) as UserIdentity[];
} catch (error: any) {
console.error('[IdentityService] Failed to get identities:', error);
logger.error('Failed to get user identities', {
action: 'get_identities',
error: error.message
});
return [];
}
}
@@ -124,7 +128,11 @@ export async function disconnectIdentity(
return { success: true };
} catch (error: any) {
console.error('[IdentityService] Failed to disconnect identity:', error);
logger.error('Failed to disconnect identity', {
action: 'identity_disconnect',
provider,
error: error.message
});
return {
success: false,
error: error.message || 'Failed to disconnect identity'
@@ -152,7 +160,11 @@ export async function connectIdentity(
return { success: true };
} catch (error: any) {
console.error('[IdentityService] Failed to connect identity:', error);
logger.error('Failed to connect identity', {
action: 'identity_connect',
provider,
error: error.message
});
return {
success: false,
error: error.message || `Failed to connect ${provider} account`
@@ -177,7 +189,10 @@ export async function addPasswordToAccount(): Promise<IdentityOperationResult> {
};
}
console.log('[IdentityService] Sending password reset email');
logger.info('Initiating password setup', {
action: 'password_setup_initiated',
email: userEmail
});
// Trigger Supabase password reset email
// User clicks link and sets password, which automatically creates email identity
@@ -189,11 +204,19 @@ export async function addPasswordToAccount(): Promise<IdentityOperationResult> {
);
if (resetError) {
console.error('[IdentityService] Failed to send password reset email:', resetError);
logger.error('Failed to send password reset email', {
userId: user?.id,
action: 'password_setup_email',
error: resetError.message
});
throw resetError;
}
console.log('[IdentityService] Password reset email sent successfully');
logger.info('Password reset email sent', {
userId: user!.id,
action: 'password_setup_initiated',
email: userEmail
});
// Log the action
await logIdentityChange(user!.id, 'password_setup_initiated', {
@@ -207,7 +230,10 @@ export async function addPasswordToAccount(): Promise<IdentityOperationResult> {
email: userEmail
};
} catch (error: any) {
console.error('[IdentityService] Failed to initiate password setup:', error);
logger.error('Failed to initiate password setup', {
action: 'password_setup',
error: error.message
});
return {
success: false,
error: error.message || 'Failed to send password reset email'
@@ -232,7 +258,11 @@ async function logIdentityChange(
_details: details
});
} catch (error) {
console.error('[IdentityService] Failed to log audit event:', error);
logger.error('Failed to log identity change to audit', {
userId,
action,
error: error instanceof Error ? error.message : String(error)
});
// Don't fail the operation if audit logging fails
}
}