save method

Future<void> save({
  1. required String uid,
  2. required String token,
  3. String? role,
  4. String? mfaCode,
  5. DateTime? mfaCodeExpiry,
  6. Map<String, dynamic>? custom,
})

Implementation

Future<void> save({
  required String uid,
  required String token,
  String? role,
  String? mfaCode,
  DateTime? mfaCodeExpiry,
  Map<String, dynamic>? custom,
}) async {
  try {
    final writes = <Future>[];

    writes.add(_storage.write(key: '${_authPrefix}uid', value: uid));
    writes.add(_storage.write(key: '${_authPrefix}token', value: token));

    if (role != null) {
      writes.add(_storage.write(key: '${_authPrefix}role', value: role));
    }

    if (mfaCode != null) {
      writes.add(_storage.write(key: '${_authPrefix}mfa_code', value: mfaCode));
    }

    if (mfaCodeExpiry != null) {
      writes.add(_storage.write(
          key: '${_authPrefix}mfa_expiry',
          value: mfaCodeExpiry.toIso8601String()
      ));
    }

    if (custom != null && custom.isNotEmpty) {
      writes.add(_storage.write(
          key: '${_authPrefix}custom',
          value: jsonEncode(custom)
      ));
    }

    await Future.wait(writes);

    batch(() {
      this.uid.value = uid;
      this.token.value = token;
      if (role != null) this.role.value = role;
      if (mfaCode != null) this.mfaCode.value = mfaCode;
      if (mfaCodeExpiry != null) this.mfaCodeExpiry.value = mfaCodeExpiry;
      if (custom != null) customFields.value = custom;
    });
  } catch (e) {
    throw StorageException(
      'Error guardando autenticación',
      details: e.toString(),
      type: StorageType.auth,
    );
  }
}