loggedInUserDisplayName method

  1. @override
Future<String?> loggedInUserDisplayName()
override

Retrieves the display name of the currently logged-in user.

Implementation

@override
Future<String?> loggedInUserDisplayName() {
  return _executeRequest(
    () async {
      final accessToken = await _getAccessToken();
      if (accessToken.isEmpty) return null;
      // Fetch user profile info from the Microsoft Graph `/me` endpoint.
      final response = await http.get(
        Uri.parse('https://graph.microsoft.com/v1.0/me'),
        headers: {'Authorization': 'Bearer $accessToken'},
      );
      if (response.statusCode != 200) return null;
      final json = jsonDecode(response.body);
      // Prefer `displayName`, but fall back to `userPrincipalName` if it's not available.
      String? name = json['displayName'] as String?;
      if (name?.trim().isEmpty ?? true) {
        name = json['userPrincipalName'] as String?;
      }
      return name;
    },
    operation: 'loggedInUserDisplayName',
  );
}