Google 身份验证和 Facebook 身份验证现有用户

时间:2020-12-31 09:08:39

标签: flutter dart firebase-authentication google-authentication facebook-authentication

大家好,如果可以,请给我一些帮助。我不确定这是 Firebase 方面的问题,还是我的代码或正确配置 Firebase 身份验证失败。

问题来了。 我想看看 Firestore 中是否存在用户。如果是,则链接帐户。我阅读了文档并了解到,在使用 SigninwithCredential() 时,它应该会抛出一个错误,指出“错误用户已经存在于 FIRESTORE”这一行。

因此,在尝试处理错误之前,进行了类似的测试以查看是否会出现错误。

  1. 继续使用 Facebook(确定)。
  2. 已登录(确定)。
  3. 继续使用 Google(预期错误)(未抛出错误) 所以这里发生的事情是最初由 Facebook 创建的用户被 Google 覆盖了。
  4. 使用 Facebook 登录(预期错误)(正常)按预期收到错误“帐户已存在,但使用不同的登录凭据”
  5. 多次阅读文档并将谷歌代码与 Facebook 代码进行比较,但似乎无法找到答案。
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** ```

0 个答案:

没有答案