signInWithFacebook method

Future<SocialLoginDataModel?> signInWithFacebook()

Implementation

Future<SocialLoginDataModel?> signInWithFacebook() async {
  try {
    final LoginResult result = await FacebookAuth.instance
        .login(); // by default we request the email and the public profile
// or FacebookAuth.i.login()
    if (result.status == LoginStatus.success) {
      // you are logged
      AccessToken? accessToken = result.accessToken;
      Map<String, dynamic> userData =
          await FacebookAuth.instance.getUserData();
      print("userData : ${userData}");
      print("accessToken : ${accessToken?.token ?? ""}");

      final OAuthCredential credential =
          FacebookAuthProvider.credential(accessToken?.token ?? "");
      // Once signed in, return the UserCredential
      final userCredential =
          await FirebaseAuth.instance.signInWithCredential(credential);
      User? firebaseUser = userCredential.user;

      return SocialLoginDataModel(
          socialId: firebaseUser?.providerData.first.uid ?? "",
          firebaseAuthUser: firebaseUser,
          errorMessage: _successConst,
          status: true);
    } else {
      return SocialLoginDataModel(
          socialId: null,
          firebaseAuthUser: null,
          errorMessage: result.message ?? _failureConst,
          status: false);
    }
  }  catch (e) {
    log("e---->${e}");
    displayError(e.toString());
    return SocialLoginDataModel(
        socialId: null,
        firebaseAuthUser: null,
        errorMessage: "${e.toString()}",
        status: false);
  }
}