signInByGoogle method

  1. @override
Future<AuthResponse<T>> signInByGoogle({
  1. OAuthAuthenticator? authenticator,
  2. bool storeToken = false,
})
override

Implementation

@override
Future<AuthResponse<T>> signInByGoogle({
  OAuthAuthenticator? authenticator,
  bool storeToken = false,
}) async {
  try {
    emit(const AuthResponse.loading(AuthProviders.google, AuthType.oauth));
    final response = await authHandler.signInWithGoogle();
    final raw = response.data;
    if (raw != null && raw.credential != null) {
      final current = await authHandler.signInWithCredential(
        credential: raw.credential!,
      );
      if (current.isSuccessful) {
        final result = current.data?.user;
        if (result != null) {
          final user = (authenticator ?? Authenticator.empty()).copy(
            id: result.uid,
            accessToken: storeToken ? raw.accessToken : null,
            idToken: storeToken ? raw.idToken : null,
            email: raw.email ?? result.email,
            name: raw.name ?? result.displayName,
            phone: result.phoneNumber,
            photo: raw.photo ?? result.photoURL,
            provider: AuthProviders.google.name,
            loggedIn: true,
            loggedInTime: Entity.generateTimeMills,
            verified: true,
          );
          return _update(
            id: user.id,
            creates: user.source,
            updates: {
              ...user.extra ?? {},
              AuthKeys.i.loggedIn: true,
              AuthKeys.i.loggedInTime: Entity.generateTimeMills,
            },
          ).then((value) {
            return emit(AuthResponse.authenticated(
              value,
              msg: msg.signInWithGoogle.done,
              provider: AuthProviders.google,
              type: AuthType.oauth,
            ));
          });
        } else {
          return emit(AuthResponse.failure(
            msg.authorization,
            provider: AuthProviders.google,
            type: AuthType.oauth,
          ));
        }
      } else {
        return emit(AuthResponse.failure(
          current.exception,
          provider: AuthProviders.google,
          type: AuthType.oauth,
        ));
      }
    } else {
      return emit(AuthResponse.failure(
        response.exception,
        provider: AuthProviders.google,
        type: AuthType.oauth,
      ));
    }
  } catch (_) {
    return emit(AuthResponse.failure(
      msg.signInWithGoogle.failure ?? _,
      provider: AuthProviders.google,
      type: AuthType.oauth,
    ));
  }
}