startAuthenticationFlow method

  1. @override
Future<bool> startAuthenticationFlow({
  1. String? deviceCode,
  2. bool useDeviceCode = true,
})
override

Implementation

@override
Future<bool> startAuthenticationFlow({
  String? deviceCode,
  bool useDeviceCode = true,
}) async {
  try {
    if (useDeviceCode) {
      if (deviceCode == null) {
        final response = await _microsoftAuthService.getDeviceCode();
        if (onGetDeviceCode != null) {
          onGetDeviceCode!(response);
        }
        deviceCode = response.deviceCode;
      }
      _accessToken = await _microsoftAuthService.getAccessToken(deviceCode);
      _microsoftRefreshToken = _microsoftAuthService.getRefreshToken();
    } else {
      _accessToken =
          await _microsoftAuthService.getAccessTokenWithLocalServer();
      _microsoftRefreshToken = _microsoftAuthService.getRefreshToken();
    }

    final xstsResponse = await _xboxAuthService.getXstsToken(_accessToken!);
    final xstsToken = xstsResponse['token']!;
    final uhs = xstsResponse['uhs']!;
    _xstsToken = xstsToken;
    _xstsUhs = uhs;
    final tokenResponse = await _minecraftAuthService.getMinecraftAccessToken(
      uhs,
      xstsToken,
    );
    _minecraftToken = tokenResponse.accessToken;

    try {
      _minecraftProfile = await _minecraftAuthService.getAccountprofile();
    } catch (e) {
      debugPrint('Failed to get Minecraft profile: $e');
    }

    return true;
  } catch (e) {
    debugPrint('Authentication flow failed: $e');
    return false;
  }
}