init static method

Future<User?> init({
  1. String? baseUrl,
  2. required String appToken,
  3. RefreshOptions? refreshOptions,
  4. StorageInterface? storage,
})

Implementation

static Future<User?> init({
  String? baseUrl,
  required String appToken,
  RefreshOptions? refreshOptions,
  common.StorageInterface? storage,
}) async {
  if (_api != null) {
    //no Init, only once
    try {
      return await getActualUser(jwt: true);
    } catch (e) {
      return null;
    }
  }

  //load the ffi lib
  const base = "sentc_light_flutter";
  final path = Platform.isWindows ? "$base.dll" : "lib$base.so";
  late final dylib = Platform.isIOS
      ? DynamicLibrary.process()
      : Platform.isMacOS
          ? DynamicLibrary.executable()
          : DynamicLibrary.open(path);

  final SentcFlutterRustLightImpl api = SentcFlutterRustLightImpl(dylib);
  Sentc.baseUrl = baseUrl ?? "https://api.sentc.com";

  RefreshOption refreshEndpoint = refreshOptions != null ? refreshOptions.endpoint : RefreshOption.api;

  var refreshEndpointFn = refreshOptions != null
      ? refreshOptions.endpointFn ??
          (String oldJwt) async {
            return "";
          }
      : (String oldJwt) async {
          return "";
        };

  _api = api;
  Sentc.appToken = appToken;
  Sentc.refreshEndpoint = refreshEndpoint;
  _endpointFn = refreshEndpointFn;

  _storage = storage ?? common.SharedPreferencesStorage();
  await _storage!.init();

  try {
    final user = await getActualUser();

    if (refreshEndpoint == RefreshOption.api) {
      //do init only when refresh endpoint is api
      final out = await getApi().initUser(
        baseUrl: Sentc.baseUrl,
        authToken: Sentc.appToken,
        jwt: user.jwt,
        refreshToken: user.refreshToken,
      );

      user.jwt = out.jwt;
      user.groupInvites = out.invites;
    } else {
      //do normal refresh (maybe with another strategy)
      await user.getJwt();

      return user;
    }
  } catch (e) {
    return null;
  }

  return null;
}