clearKwikpassSession static method

Future<bool> clearKwikpassSession()

Implementation

static Future<bool> clearKwikpassSession() async {
  try {
    // Sent before the tokens are dropped; skipped when no phone is known,
    // and a failed event must not block logout.
    try {
      await _sendPhoneEvent({
        'category': 'logged_in_page',
        'action': 'logged_out',
        'label': 'logout_button_click',
      }, await getUserPhone());
    } catch (error) {
      if (kDebugMode) {
        debugPrint('Failed to send logout event to snowplow: $error');
      }
    }

    // await trackAnalyticsEvent(AnalyticsEvents.appLogout, {
    //   'email': parsedUser['email']?.toString() ?? "",
    //   'phone': phone,
    //   'customer_id': parsedUser['shopifyCustomerId']?.toString() ?? "",
    // });

    final env = await cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkEnvironmentKey)!);
    final mid = await cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkMerchantIdKey)!) ?? '';
    final merchantTypeStr = await cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkMerchantTypeKey)!);
    final merchantType = MerchantType.values.firstWhere(
      (e) => e.name == merchantTypeStr,
      orElse: () => MerchantType.shopify,
    );
    final isSnowplowTrackingEnabled =
        await cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.isSnowplowTrackingEnabled)!) ==
            'true';

    final isKpEnabled =
        await cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkKPEnabled)!) ==
            'true';
            final isCheckoutEnabled =
        await cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkCheckoutEnabled)!) ==
            'true';

            final mode =
        await cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkMode)!);
    final integrationType = await cacheInstance
        .getValue(cdnConfigInstance.getKeys(StorageKeyKeys.integrationType)!);

    await cacheInstance.clearCache();
    // final prefs = await SharedPreferences.getInstance();
    // await prefs.remove(KeyConfig.gkCoreTokenKey);
    // await prefs.remove(KeyConfig.gkAccessTokenKey);
    // await prefs.remove(KeyConfig.gkVerifiedUserKey);
    // await prefs.remove(KeyConfig.gkRequestIdKey);
    // await prefs.remove(KeyConfig.kpRequestIdKey);
    // await prefs.remove(KeyConfig.gkAuthTokenKey);
    await SecureStorage.clearAllSecureData();

    // Both singletons hold per-user state that clearing storage can't reach:
    // Dio keeps the auth headers, snowplow keeps the user id.
    DioClient().dispose();
    await SnowplowClient.reset();

    await initializeSdk(InitializeSdkProps(
      mid: mid,
      environment: Environment.values.firstWhere((e) => e.name == env,
          orElse: () => Environment.production),
      merchantType: merchantType,
      // APP_MAKER is stored for shopify/customShopify; either derives it again.
      integrationType: IntegrationType.fromValue(integrationType) ??
          IntegrationType.shopify,
      isSnowplowTrackingEnabled: isSnowplowTrackingEnabled,
      mode: mode ?? "",
      settings: Settings(
        enableKwikPass: isKpEnabled,
        enableCheckout: isCheckoutEnabled,
      ),
    ));

    // Logger().clearLogs();

    return true;
  } catch (error) {
    throw handleApiError(error);
  }
}