useAuthEmulator method

  1. @override
Future<void> useAuthEmulator(
  1. String host,
  2. int port
)

Changes this instance to point to an Auth emulator running locally.

Set the host and port of the local emulator, such as "localhost" with port 9099

Note: Must be called immediately, prior to accessing auth methods. Do not use with production credentials as emulator traffic is not encrypted.

Implementation

@override
Future<void> useAuthEmulator(String host, int port) async {
  try {
    // Get current session storage value
    final String? emulatorOrigin =
        web.window.sessionStorage.getItem(getOriginName(delegate.app.name));

    // The generic platform interface is with host and port split to
    // centralize logic between android/ios native, but web takes the
    // origin as a single string
    final String origin = 'http://$host:$port';

    if (origin == emulatorOrigin) {
      // If the origin is the same as the current one, do nothing
      // The emulator was already started at the app start
      return;
    }

    delegate.useAuthEmulator(origin);
    // Save to session storage so that the emulator is used on refresh
    // only in debug mode
    if (kDebugMode) {
      web.window.sessionStorage
          .setItem(getOriginName(delegate.app.name), origin);
    }
  } catch (e) {
    if (e is auth_interop.AuthError) {
      final String code = e.code.toDart;
      // this catches Firebase Error from web that occurs after hot reloading & hot restarting
      if (code != 'auth/emulator-config-failed') {
        throw getFirebaseAuthException(e);
      }
    } else {
      rethrow;
    }
  }
}