signInAnonymously method

Future<AuthResponse<T>> signInAnonymously({
  1. GuestAuthenticator? authenticator,
  2. Object? args,
  3. String? id,
  4. bool notifiable = true,
})

Implementation

Future<AuthResponse<T>> signInAnonymously({
  GuestAuthenticator? authenticator,
  Object? args,
  String? id,
  bool notifiable = true,
}) async {
  try {
    emit(
      const AuthResponse.loading(Provider.email, AuthType.login),
      args: args,
      id: id,
      notifiable: notifiable,
    );

    final response = await delegate.signInAnonymously();
    if (!response.isSuccessful) {
      return emit(
        AuthResponse.failure(
          response.error,
          provider: Provider.guest,
          type: AuthType.none,
        ),
        args: args,
        id: id,
        notifiable: notifiable,
      );
    }

    final result = response.data;
    if (result == null) {
      return emit(
        AuthResponse.failure(
          msg.authorization,
          provider: Provider.guest,
          type: AuthType.none,
        ),
        args: args,
        id: id,
        notifiable: notifiable,
      );
    }

    final user = (authenticator ?? Authenticator.guest()).update(
      id: Modifier(result.uid),
      anonymous: Modifier(result.isAnonymous),
      email: Modifier(result.email),
      name: Modifier(result.displayName),
      phone: Modifier(result.phoneNumber),
      photo: Modifier(result.photoURL),
      loggedIn: Modifier(true),
      loggedInTime: Modifier(EntityHelper.generateTimeMills),
    );
    final value = await _update(
      id: user.id,
      initials: user.filtered,
      updates: {
        if (authenticator?.extra != null) ...authenticator!.extra!,
        AuthKeys.i.loggedIn: true,
        AuthKeys.i.loggedInTime: EntityHelper.generateTimeMills,
        AuthKeys.i.anonymous: result.isAnonymous,
        AuthKeys.i.email: result.email,
        AuthKeys.i.name: result.displayName,
        AuthKeys.i.password: authenticator?.password,
        AuthKeys.i.phone: result.phoneNumber,
        AuthKeys.i.photo: result.photoURL,
        AuthKeys.i.provider: Provider.guest.id,
        AuthKeys.i.username: authenticator?.username,
        AuthKeys.i.verified: result.emailVerified,
      },
    );
    return emit(
      AuthResponse.authenticated(
        value,
        msg: msg.signInWithEmail.done,
        provider: Provider.guest,
        type: AuthType.none,
      ),
      args: args,
      id: id,
      notifiable: notifiable,
    );
  } catch (error) {
    return emit(
      AuthResponse.failure(
        msg.signInWithEmail.failure ?? error,
        provider: Provider.guest,
        type: AuthType.none,
      ),
      args: args,
      id: id,
      notifiable: notifiable,
    );
  }
}