refreshAuthenticationWithToken method

  1. @override
Future<MicrosoftAccount?> refreshAuthenticationWithToken(
  1. String refreshToken
)
override

Refreshes authentication using a specific refresh token and returns the Microsoft account profile

This method allows refreshing authentication for any Microsoft account by providing its refresh token, and returns the Microsoft account profile information.

@param refreshToken The Microsoft refresh token to use for the refresh operation @return A Future that resolves to the Microsoft account profile, or null if the refresh operation failed @override

Implementation

@override
Future<MicrosoftAccount?> refreshAuthenticationWithToken(
  String refreshToken,
) async {
  try {
    // Get Microsoft account profile with refreshed tokens
    final microsoftAccount = await _microsoftAuthService
        .refreshAccessTokenWithProfile(refreshToken);

    if (microsoftAccount == null || microsoftAccount.refreshToken.isEmpty) {
      return null;
    }

    // Update the cached refresh token
    _microsoftRefreshToken = microsoftAccount.refreshToken;

    // Get access token
    _accessToken = await _microsoftAuthService.refreshAccessToken(
      microsoftAccount.refreshToken,
    );
    if (_accessToken == null) {
      return null;
    }

    // Get XSTS token and UHS with the refreshed access token
    try {
      final xstsResponse = await _xboxAuthService.getXstsToken(_accessToken!);
      final xstsToken = xstsResponse['token']!;
      final uhs = xstsResponse['uhs']!;

      // Update XSTS token and UHS
      _xstsToken = xstsToken;
      _xstsUhs = uhs;

      debugPrint('Successfully updated XSTS token and UHS during refresh');
    } catch (e) {
      debugPrint('Failed to get XSTS token during refresh: $e');
      // Don't return null here, as we still want to return the Microsoft account
    }

    return microsoftAccount;
  } catch (e) {
    debugPrint('Failed to refresh authentication with token: $e');
    return null;
  }
}