signInWithCustomToken method

  1. @override
Future<AuthUser<User>> signInWithCustomToken({
  1. required String token,
})
override

Implementation

@override
Future<AuthUser<User>> signInWithCustomToken({required String token}) async {
  final MonitoringService monitoringService = core.get<MonitoringService>();
  var trace =
      await monitoringService.createTrace(name: 'signInWithCustomToken');
  trace.start();
  try {
    if (!isReady) {
      throw LittlefishFirebaseAuthException('AUTH_INIT_000',
          '$providerName: Firebase is not initialized, $providerName cannot sign in with custom token');
    }

    if (token.isEmpty) {
      throw LittlefishFirebaseAuthException('AUTH_004',
          '$providerName: Custom token is empty, $providerName cannot sign in with custom token');
    }

    logger.debug(
      this,
      "$providerName: Signing in with custom token",
    );

    var r = await kInstance?.signInWithCustomToken(token);

    logger.info(
      this,
      "$providerName: Signed in with custom token",
    );

    if (r == null) {
      throw LittlefishFirebaseAuthException('AUTH_005',
          '$providerName: Sign in with custom token failed, $providerName cannot sign in with custom token');
    }

    if (r.user == null) {
      throw LittlefishFirebaseAuthException('AUTH_006',
          '$providerName: Sign in with custom token failed, $providerName cannot locate user information post-logon');
    }

    kUserCredential = r.credential;

    logger.info(
      this,
      "$providerName: Logged user on successfully with custom token",
    );

    return FirebaseAuthUser(
      kUser: r.user!,
      additionalUserInfo: _convertAdditionalUserInfo(r.additionalUserInfo),
      baseUserId: r.user?.uid ?? '',
      baseUserFirstName: r.user?.displayName ?? '',
      baseUserLastName: r.user?.displayName ?? '',
      baseUserEmail: r.user?.email ?? '',
      baseUserRegisteredDate: r.user?.metadata.creationTime ?? DateTime.now(),
      baseUserMobileNumber: r.user?.phoneNumber ?? '',
      baseUserProfileImageUri: r.user?.photoURL ?? '',
      baseUserGender: UserGender.notSpecified,
      baseUserDisplayName: r.user?.displayName ?? '',
      baseUserEmailVerified: r.user?.emailVerified ?? false,
    );
  } on FirebaseAuthException catch (e) {
    throw _handleFirebaseAuthException(e);
  } catch (e) {
    if (e is LittlefishException) {
      rethrow;
    }
    throw LittlefishFirebaseAuthException(
      'AUTH_014',
      'An unexpected error occurred during custom token sign-in.',
      cause: e,
    );
  } finally {
    trace.stop();
  }
}