Flutter 如何将多个身份验证提供商链接到 Firebase 帐户?

piy*_*ain 5 dart firebase firebase-authentication flutter

我在我的 flutter 应用程序中使用 firebase。在我的应用程序中,用户可以使用 3 种方式登录或注册。

  1. 电子邮件 ID 和密码。
  2. 谷歌
  3. Facebook。

这是我的 Firebase 设置。 在此输入图像描述

这是我的用户收藏。 在此输入图像描述

现在我的疑问是如何为具有相同电子邮件 ID 的用户链接多个身份验证提供商?

我使用 Uid 将用户信息存储到用户集合中。如果我为多个提供商启用多个帐户,如何将用户数据存储在同一个文档中?

我在谷歌上搜索了很多,但没有找到合适的解决方案。

对于使用电子邮件 ID 进行注册,我使用的是这个。

  final newUser = await _auth.createUserWithEmailAndPassword(
          email: widget.email, password: widget.pass);
Run Code Online (Sandbox Code Playgroud)

然后我将用户的数据存储到用户集合中。(在这个表格中,我还有一种表格,我可以从其中获取其他数据)。

谷歌登录代码

  Future<User> signInWithGoogle() async {
    // Trigger the authentication flow
    final GoogleSignInAccount googleUser = await _googleSignIn.signIn();

    // Obtain the auth details from the request
    final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

    // Create a new credential
    final GoogleAuthCredential credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    // Once signed in, return the UserCredential
    final UserCredential authResult = await _firebaseAuth.signInWithCredential(credential);
    final User user = authResult.user;

    print('authResult');
    print(authResult);

    return user;
  }
Run Code Online (Sandbox Code Playgroud)

Mαπ*_*π.0 0

实际上,通过将身份验证提供程序凭据链接到现有用户帐户,用户可以使用多个身份验证提供程序登录您的应用程序。

您可以按照此处的文档了解将身份验证提供者凭据链接到现有用户帐户的过程:

我建议还访问Firebase for Flutter 的官方文档