signInWithFacebook method

Future<UserCredential> signInWithFacebook()

Signs in the user using Facebook authentication and returns the user credential.

Implementation

Future<UserCredential> signInWithFacebook() async {
  try {
    if (_auth.currentUser != null && _auth.currentUser!.isAnonymous != true) {
      await _auth.signOut();
    }
    if (kIsWeb) {
      // Create a new provider
      FacebookAuthProvider facebookProvider = FacebookAuthProvider();
      facebookProvider.addScope('email');
      facebookProvider.setCustomParameters({
        'display': 'popup',
      });

      // Once signed in, return the UserCredential
      final credential = await _auth.signInWithPopup(facebookProvider);
      return credential;
    } else {
      final rawNonce = generateNonce();
      final nonce = sha256ofString(rawNonce);

      // Trigger the sign-in flow
      final LoginResult loginResult = await FacebookAuth.instance
          .login(loginTracking: LoginTracking.limited, nonce: nonce);
      switch (loginResult.status) {
        case LoginStatus.success:
          {
            final AuthCredential facebookAuthCredential;

            switch (loginResult.accessToken!.type) {
              case AccessTokenType.classic:
                final token = loginResult.accessToken! as ClassicToken;
                facebookAuthCredential = FacebookAuthProvider.credential(
                  token.authenticationToken!,
                );
                break;
              case AccessTokenType.limited:
                final token = loginResult.accessToken! as LimitedToken;
                facebookAuthCredential = OAuthCredential(
                  providerId: 'facebook.com',
                  signInMethod: 'oauth',
                  idToken: token.tokenString,
                  rawNonce: rawNonce,
                );
                break;
            }

            if (_auth.currentUser != null &&
                _auth.currentUser!.isAnonymous == true) {
              final credential = _auth.currentUser!
                  .linkWithCredential(facebookAuthCredential);
              await _auth.currentUser!.reload();
              if (_auth.currentUser!.email is String &&
                  _auth.currentUser!.email!.isNotEmpty) {
                await resendEmailVerification(_auth.currentUser!.email!);
              }
              return credential;
            }
            // Once signed in, return the UserCredential
            final credential =
                await _auth.signInWithCredential(facebookAuthCredential);
            return credential;
          }
        case LoginStatus.cancelled:
          throw AuthDataServiceException(
              message: 'Facebook Sign In Cancelled', code: RdevCode.Aborted);

        default:
          throw AuthDataServiceException(
            message: 'Facebook Sign In Failed',
          );
      }
    }
  } catch (err) {
    if (err is FirebaseAuthException) {
      throw AuthDataServiceException.fromRdevException(err.toRdevException());
    }
    throw AuthDataServiceException(
      message: err.toString(),
    );
  }
}