AuthyoWidgetConfig.fromJson constructor

AuthyoWidgetConfig.fromJson(
  1. Map<String, dynamic> json
)

Implementation

factory AuthyoWidgetConfig.fromJson(Map<String, dynamic> json) {
  // Unwrap the API envelope if present.
  final data = json['data'] is Map<String, dynamic>
      ? json['data'] as Map<String, dynamic>
      : json;

  Map<String, dynamic>? customization;
  for (final key in ['customization', 'appCustomizationModel']) {
    if (data[key] is Map) {
      customization = Map<String, dynamic>.from(data[key] as Map);
      break;
    }
  }
  // Bare AppCustomizationModel (getappcustomization).
  customization ??= data.containsKey('buttonColor') ? data : null;

  final methods = <String>[];
  final socials = <AuthyoSocialLogin>[];

  final rawMethods = data['authMethods'];
  if (rawMethods is List) {
    methods.addAll(rawMethods.map((e) => e.toString()));
  }
  final rawSocials = data['socialLogins'];
  if (rawSocials is List) {
    for (final s in rawSocials) {
      if (s is Map) {
        socials.add(AuthyoSocialLogin.fromJson(Map<String, dynamic>.from(s)));
      }
    }
  }
  // Legacy CDN shape: results mixes "Sms" and "Google,123".
  final legacy = data['results'];
  if (legacy is List) {
    for (final e in legacy) {
      final s = e.toString();
      final social = AuthyoSocialLogin.fromLegacyEntry(s);
      if (social != null) {
        socials.add(social);
      } else if (!s.contains(',')) {
        methods.add(s);
      }
    }
  }

  final styleRaw = data['designStyle'] ?? customization?['designStyle'];
  final otpRaw = data['otpLength'];

  return AuthyoWidgetConfig(
    designStyle: styleRaw == null
        ? null
        : AuthyoDesignStyle.fromValue(styleRaw),
    theme: AuthyoTheme.fromCustomization(customization),
    authMethods: methods,
    socialLogins: socials,
    otpLength: otpRaw is num ? otpRaw.toInt() : int.tryParse('$otpRaw'),
    raw: json,
  );
}