0

Hello Everyone someone please offer me some help if you can. I'm not sure if this is an issue on Firebase's side or if I failed on my code or configuring the Firebase authentication correctly.

So here's the issue. I wanted to see if a user existed inside the firestore. If it does then link the accounts. I read the documentation and understand that when using the SigninwithCredential() it should throw an error saying something on the lines of "ERROR USER ALREADY EXISTS IN FIRESTORE".

So testing went something like this to see if I'd get an error before trying to handle the error.

  1. Continue with Facebook (OK).
  2. Signed In (OK).
  3. Continue with Google (Expected Error) (Didn't throw Error) So what happened here instead was the user originally created by Facebook was overridden by Google.
  4. Sign In with Facebook (Expected Error) (OK) Recieved error as expected "An Account already exists but with different sign-in credentials"
  5. Read documentation several times and compared google code with Facebook code and can't seem to find answer.
class MyFirebase {
  static bool isSignIn = false;
  static auth.FirebaseAuth _auth = auth.FirebaseAuth.instance;
  static FacebookLogin facebookLogin = FacebookLogin();
  static GoogleSignIn googleSignIn = GoogleSignIn();

  static Future<auth.User> loginGoogle() async {
    final prefs = await SharedPreferences.getInstance();
    final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
    if (googleSignInAccount == null) {
      print("ERROR HAS OCCURED WITH THE GOOGLE LOGIN");
      throw new Exception("ERROR HAS OCCURED WITH THE LOGIN");
    } else {
      final GoogleSignInAuthentication googleSignInAuthentication =
          await googleSignInAccount.authentication;

      var item = googleSignInAccount.photoUrl;
      print("PHOTO URL" + item);
      prefs.setString("photoURL", item);

      final auth.AuthCredential credential = auth.GoogleAuthProvider.credential(
          accessToken: googleSignInAuthentication.accessToken);

      try {
        var authResult = await _auth.signInWithCredential(credential);

        return authResult.user;
      } catch (e) {
        print(e);
        throw (e);
      }
    }
  }

  static Future<auth.User> loginFacebook() async {
    final FacebookLoginResult result = await facebookLogin.logIn(['email']);
    final prefs = await SharedPreferences.getInstance();
    var a;
    switch (result.status) {
      case FacebookLoginStatus.cancelledByUser:
        print("CANCELLED BY USER DO NOT RETURN");
        throw new Exception("CANCELLED BY USER DO NOT RETURN");
        break;
      case FacebookLoginStatus.error:
        print("ERROR OCCURED");
        throw new Exception("ERROR oCCURED");
        break;
      case FacebookLoginStatus.loggedIn:
        try {
          final FacebookAccessToken accessToken = result.accessToken;
          auth.AuthCredential credential =
              auth.FacebookAuthProvider.credential(accessToken.token);
          print("########## MAKING GRAPH RESPONSE");
          final response = await http.get(
              'https://graph.facebook.com/v2.12/me?fields=name,first_name,picture.width(800).height(800),last_name,email&access_token=${accessToken.token}');
          final profile = jsonDecode(response.body);
          String item = profile['picture']['data']['url'];
          prefs.setString("photoURL", item);
          a = await _auth.signInWithCredential(credential);
          return a.user;
        } catch (e) {
          print(e);
          throw e;
        }
        break;
    }
    return null;
  }

  static Future<void> signOut() async {
    await facebookLogin.logOut();
    await _auth.signOut();
    await googleSignIn.signOut();
  }
}
**strong text** ```
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I'm not sure I understand your description. Can you point out the one line/API call in your code that doesn't do what you expect it to do? – Frank van Puffelen Dec 31 '20 at 16:52
  • So this is a security issue. Google is safer so it deletes the other Facebook login. Here's a link I found https://groups.google.com/g/firebase-talk/c/ms_NVQem_Cw/m/8g7BFk1IAAAJ?pli=1 – Terry Phillips Jan 09 '21 at 04:18
  • I did not understand that from your question, but it sounds like you ran into preferred providers. The post you found is indeed authoritative. Also see: https://stackoverflow.com/questions/65565874/firebase-authentication-provider-silently-changes-from-password-to-google-com/65566592#65566592 – Frank van Puffelen Jan 09 '21 at 04:40
  • Does this answer your question? [Firebase authentication provider silently changes from password to google.com](https://stackoverflow.com/questions/65565874/firebase-authentication-provider-silently-changes-from-password-to-google-com) – Frank van Puffelen Jan 09 '21 at 04:40

0 Answers0