codelesslyWithToken function

Future<Codelessly> codelesslyWithToken(
  1. String authToken, {
  2. CodelesslyConfig config = const CodelesslyConfig(),
  3. bool cloudServices = false,
  4. TestStoreMode testStore = TestStoreMode.auto,
  5. FirebaseApp? app,
  6. FirebaseFirestore? firestore,
  7. FirebaseAuth? auth,
  8. FirebaseStorage? storage,
  9. ProjectAuth? projectAuth,
  10. String? functionsBaseUrl,
  11. PublishCache? cache,
  12. FontFetcher? fonts,
  13. Map<String, Object?> data = const {},
})

A Codelessly for the project authToken names, on Codelessly's Firebase, with live updates: all an app needs is its project token.

final codelessly = await codelesslyWithToken('<project token>');
runApp(MaterialApp(home: CodelesslyWidget(codelessly: codelessly)));

It opens codelesslyFirebaseApp (unless app, or firestore and auth, are given) and returns codelesslyOnFirebase on it, with config's token set to authToken. Nothing is loaded yet: the first CodelesslyWidget (or Codelessly.initialize) signs in anonymously, exchanges the token for access to its project and reads the publish.

With cloudServices, the project's Codelessly cloud data, app user accounts, cloud files and managed paywalls (codelessly.paywall; purchases verified by Codelessly) work too (codelesslyCloudServices): the token is authenticated first (the project's id is needed), so the future completes with an error when that fails. Ignored when config has its own services. testStore is when their paywalls run on the test store (Services.testStore; TestStoreMode.force for a run preview).

A project on another Codelessly backend (a staging one) gives that backend's Firebase app and its functionsBaseUrl (ProjectAuthConfig.functionsBaseUrl).

On Linux, where Firebase doesn't run, this throws an UnsupportedError: load by slug or from a bundle there.

Implementation

Future<Codelessly> codelesslyWithToken(
  String authToken, {
  CodelesslyConfig config = const CodelesslyConfig(),
  bool cloudServices = false,
  TestStoreMode testStore = TestStoreMode.auto,
  FirebaseApp? app,
  FirebaseFirestore? firestore,
  FirebaseAuth? auth,
  FirebaseStorage? storage,
  ProjectAuth? projectAuth,
  String? functionsBaseUrl,
  PublishCache? cache,
  FontFetcher? fonts,
  Map<String, Object?> data = const {},
}) async {
  if (authToken.isEmpty) throw ArgumentError.value(authToken, 'authToken', 'A project token is needed');
  if (app == null && (firestore == null || (auth == null && projectAuth == null))) {
    app = await codelesslyFirebaseApp();
  }
  final db = firestore ?? FirebaseFirestore.instanceFor(app: app!);
  var resolved = config.copyWith(authToken: authToken);
  final access = projectAuth ??
      FirebaseProjectAuth(
        config: functionsBaseUrl == null
            ? ProjectAuthConfig(authToken: authToken, slug: config.slug)
            : ProjectAuthConfig(authToken: authToken, slug: config.slug, functionsBaseUrl: functionsBaseUrl),
        auth: auth ?? FirebaseAuth.instanceFor(app: app!),
      );
  if (cloudServices && config.services == null) {
    final projectId = config.projectId ?? (await access.authenticate()).projectId;
    resolved = resolved.copyWith(
      services: codelesslyCloudServices(
        projectId: projectId,
        firestore: db,
        auth: auth ?? (app == null ? null : FirebaseAuth.instanceFor(app: app)),
        storage: storage ?? (app == null ? null : FirebaseStorage.instanceFor(app: app)),
        functions: app == null ? null : FirebaseFunctions.instanceFor(app: app),
        testStore: testStore,
      ),
    );
  }
  return codelesslyOnFirebase(
    firestore: db,
    auth: auth,
    config: resolved,
    projectAuth: access,
    cache: cache,
    fonts: fonts,
    data: data,
  );
}