我无法使用 flutter 在我的应用程序中使用多个 google 帐户登录

问题描述 投票:0回答:1

在我的应用程序中,您可以使用 google 帐户登录,但该帐户已在 firebase 中注册,并且不允许我使用其他 gmail 帐户登录。


Future<void> _signInGoogle(googleSignIn) async {
    try {
      final GoogleSignInAccount? googleUser = await googleSignIn.signIn();
      if (googleUser == null) {
        return;
      }
      final GoogleSignInAuthentication googleAuth =
          await googleUser.authentication;
      final credential = GoogleAuthProvider.credential(
          accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
      await FirebaseAuth.instance.signInWithCredential(credential);

      setState(() {
        isLoggedIn = true;
      });
      Fluttertoast.showToast(
        msg: "Login Successfull!!!",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1, 
        backgroundColor: Colors.green,
        textColor: Colors.white,
        fontSize: 16.0,
      );
    } on FirebaseAuthException catch (e) {
      Fluttertoast.showToast(
        msg: e.message.toString(),
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.green,
        textColor: Colors.white,
        fontSize: 16.0,
      );
    }
  }

我本来打算在setState中将UID清空,但是没成功。

flutter firebase firebase-authentication
1个回答
0
投票

您面临的问题与 Firebase 身份验证有关。当您使用已关联到 Firebase 中现有用户的 Google 帐户登录时,使用同一 Google 帐户进行的后续登录尝试将自动登录现有用户。将

UID
中的
setState
清空将不起作用,因为它实际上并未从 Firebase 中注销用户。

以下是允许用户使用不同 Google 帐户登录的方法:

登录前注销现有用户

使用

FirebaseAuth.instance.signOut();
实现单独的功能以从 Firebase 注销用户 在 Google 登录之前调用
Sign Out
:在您的
_signInGoogle
函数中,在启动 Google 登录过程之前,调用
sign-out
函数以确保没有现有用户登录。

Future<void> _signInGoogle(googleSignIn) async {
  // Sign out existing user if any
  await FirebaseAuth.instance.signOut();

  try {
    // Rest of your existing Google sign-in code...
  } on FirebaseAuthException catch (e) {
    // ...
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.