authenticate static method

Future<Map<String, dynamic>> authenticate({
  1. required String identityToken,
  2. required String? clientId,
  3. required String? teamId,
  4. required String? keyId,
  5. required String? privateKey,
  6. String? authorizationCode,
  7. String? userData,
})

Implementation

static Future<Map<String, dynamic>> authenticate({
  required String identityToken,
  required String? clientId,
  required String? teamId,
  required String? keyId,
  required String? privateKey,
  String? authorizationCode,
  String? userData,
}) async {
  if (clientId == null ||
      teamId == null ||
      keyId == null ||
      privateKey == null) {
    throw AuthException('Apple Sign In is not properly configured');
  }

  // Verify the identity token
  final profile = await _verifyIdentityToken(identityToken, clientId);

  // Parse user data if provided (Apple only sends this on first login)
  Map<String, dynamic>? parsedUserData;
  if (userData != null && userData.isNotEmpty) {
    try {
      parsedUserData = json.decode(userData) as Map<String, dynamic>;
    } catch (e) {
      print('Failed to parse Apple user data: $e');
    }
  }

  final email = profile['email'];
  final name = parsedUserData?['name'] != null
      ? '${parsedUserData!['name']?['firstName']} ${parsedUserData['name']?['lastName']}'
      : profile['email']?.split('@').first;

  return AuthProvider.formatUserData(
    provider: 'apple',
    providerId: profile['sub'],
    email: email,
    name: name,
    rawProfile: {
      ...profile,
      if (parsedUserData != null) 'userData': parsedUserData,
    },
  );
}