listLinkedAccounts function

Future<List<AuthLinkedAccountInfo>> listLinkedAccounts({
  1. required AuthStore store,
  2. required List<AuthProvider> providers,
  3. required String userId,
})

Lists all linked provider accounts for a user.

Uses the account store exposed by AuthStore to find linked accounts, and cross-references them with the configured providers to attach metadata like display names. No provider IDs are hardcoded.

Implementation

Future<List<AuthLinkedAccountInfo>> listLinkedAccounts({
  required AuthStore store,
  required List<AuthProvider> providers,
  required String userId,
}) async {
  final normalizedUserId = userId.trim();

  if (normalizedUserId.isEmpty) {
    throw AuthFlowException('invalid_request');
  }

  // Verify user exists
  final user = await Future.sync(() => store.users.findById(normalizedUserId));
  if (user == null) {
    throw AuthFlowException('user_not_found');
  }

  // Ask the account store for all linked accounts
  final accounts = await Future.sync(
    () => store.accounts.listForUser(normalizedUserId),
  );

  // Build a lookup from configured providers
  final providerMap = <String, AuthProvider>{};
  for (final provider in providers) {
    providerMap[provider.id] = provider;
  }

  return accounts.map((account) {
    final provider = providerMap[account.providerId];
    final metadata = account.metadata;
    return AuthLinkedAccountInfo(
      providerId: account.providerId,
      providerAccountId: account.providerAccountId,
      linkedAt: _parseLinkedAt(metadata),
      email: metadata['email']?.toString(),
      name: metadata['name']?.toString(),
      image: metadata['picture']?.toString() ?? metadata['image']?.toString(),
      displayName: provider?.id,
    );
  }).toList();
}