mountCredentials static method

Future<void> mountCredentials({
  1. String? userId,
  2. String? password,
})

Mounts already stored credentials, called at app startup

We can use optional parameters to mock in tests

Implementation

static Future<void> mountCredentials({
  String? userId,
  String? password,
}) async {
  String? effectiveUserId;
  String? effectivePassword;
  String? effectiveCookie;

  try {
    effectiveUserId = userId ?? await DaoSecuredString.get(_USER_ID);
    effectivePassword = password ?? await DaoSecuredString.get(_PASSWORD);
    effectiveCookie = await DaoSecuredString.get(_COOKIE);
  } on PlatformException {
    /// Decrypting the values can go wrong if, for example, the app was
    /// manually overwritten from an external apk.
    DaoSecuredString.remove(key: _USER_ID);
    DaoSecuredString.remove(key: _PASSWORD);
    DaoSecuredString.remove(key: _COOKIE);
    Logs.e('Credentials query failed, you have been logged out');
  }

  if (effectiveUserId == null || effectivePassword == null) {
    return;
  }

  final User user = User(
    userId: effectiveUserId,
    password: effectivePassword,
    cookie: effectiveCookie,
  );
  OpenFoodAPIConfiguration.globalUser = user;
}