handleTokenChanges method

Future<void> handleTokenChanges(
  1. User? fbUser
)

Implementation

Future<void> handleTokenChanges(fb_auth.User? fbUser) async {
  logd('handleTokenChanges called');

  // Call the function to get the cookie set
  // final token = await _fbAuth.currentUser!.getIdToken();
  if (fbUser == null) {
    logd('fbUser is null during handleTokenChanges');
    return;
  } else {
    logd('fbUser is NOT null during handleTokenChanges');
    // Note: refreshCurrentUser was here but is no longer needed.

    // Note: FCM token management has been moved to NotificationService.
    // Apps should subscribe to isLoggedInStream and call
    // NotificationService.initializeFcmToken() when user logs in.
  }

  //
  // Set the cookie for multi-domain auth
  //
  if (AppConfigBase.doUseBackendEmulator == false &&
      AppConfigBase.useCookieFederatedAuth == true) {
    final url = RepoHelpers.getFunctionUrl(_fbAuth.app, 'authfunctions-onsignin');
    final token = await fbUser.getIdToken();

    logd('onSignIn url: $url');
    // logd('token: $token');

    final http.Client client = http.Client();
    try {
      final response = await client.post(Uri.parse(url), body: {
        // 'Accept': '*/*',
        // 'User-Agent': 'Thunder Client (https://www.thunderclient.com)',
        // 'Authorization': 'Bearer $token',
        'token': token,
      });

      if (response.statusCode == 200) {
        logd('Cookie set successfully!!');
      } else {
        loge('Failed to set cookie!!');
      }
    } finally {
      client.close();
    }
  }
}