signInWithPlayGames method

Future<AuthResponse<T>> signInWithPlayGames({
  1. OAuthAuthenticator? authenticator,
  2. bool storeToken = false,
  3. Object? args,
  4. String? id,
  5. bool notifiable = true,
})

Implementation

Future<AuthResponse<T>> signInWithPlayGames({
  OAuthAuthenticator? authenticator,
  bool storeToken = false,
  Object? args,
  String? id,
  bool notifiable = true,
}) async {
  emit(
    args: args,
    id: id,
    notifiable: notifiable,
    const AuthResponse.loading(Provider.playGames, AuthType.oauth),
  );

  try {
    final hasAnonymous = this.hasAnonymous;
    final response = await delegate.signInWithPlayGames();
    final raw = response.data;
    if (raw == null || raw.credential == null) {
      return emit(
        args: args,
        id: id,
        notifiable: notifiable,
        AuthResponse.failure(
          response.error,
          provider: Provider.playGames,
          type: AuthType.oauth,
        ),
      );
    }

    final current = await delegate.signInWithCredential(raw.credential!);
    if (!current.isSuccessful) {
      return emit(
        args: args,
        id: id,
        notifiable: notifiable,
        AuthResponse.failure(
          current.error,
          provider: Provider.playGames,
          type: AuthType.oauth,
        ),
      );
    }

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

    final user = (authenticator ?? Authenticator.oauth()).update(
      id: Modifier(result.uid),
      accessToken:
          storeToken ? Modifier(raw.accessToken) : Modifier.nullable(),
      idToken: storeToken ? Modifier(raw.idToken) : Modifier.nullable(),
      anonymous: Modifier(raw.isAnonymous ?? result.isAnonymous),
      email: Modifier(raw.email ?? result.email),
      name: Modifier(raw.displayName ?? result.displayName),
      phone: Modifier(result.phoneNumber),
      photo: Modifier(raw.photoURL ?? result.photoURL),
      provider: Modifier(Provider.playGames),
      loggedIn: Modifier(true),
      loggedInTime: Modifier(EntityHelper.generateTimeMills),
      verified: Modifier(true),
    );

    final value = await _update(
      id: user.id,
      hasAnonymous: hasAnonymous,
      initials: user.filtered,
      updates: {
        ...authenticator?.extra ?? {},
        AuthKeys.i.loggedIn: true,
        AuthKeys.i.loggedInTime: EntityHelper.generateTimeMills,
        AuthKeys.i.anonymous: raw.isAnonymous ?? result.isAnonymous,
        AuthKeys.i.email: raw.email ?? result.email,
        AuthKeys.i.name: raw.displayName ?? result.displayName,
        AuthKeys.i.password: authenticator?.password,
        AuthKeys.i.phone: raw.phoneNumber ?? result.phoneNumber,
        AuthKeys.i.photo: raw.photoURL ?? result.photoURL,
        AuthKeys.i.provider: Provider.playGames.id,
        AuthKeys.i.username: authenticator?.username,
        AuthKeys.i.verified: raw.emailVerified ?? result.emailVerified,
      },
    );

    return emit(
      args: args,
      id: id,
      notifiable: notifiable,
      AuthResponse.authenticated(
        value,
        msg: msg.signInWithGithub.done,
        provider: Provider.playGames,
        type: AuthType.oauth,
      ),
    );
  } catch (error) {
    return emit(
      args: args,
      id: id,
      notifiable: notifiable,
      AuthResponse.failure(
        msg.signInWithGithub.failure ?? error,
        provider: Provider.playGames,
        type: AuthType.oauth,
      ),
    );
  }
}