让 flutter 应用程序强制用户使用 FirebaseAuth 和 GoogleSignInAuthentication 选择帐户

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

我想强制用户在登录时选择他的帐户之一。有什么方法可以做到这一点吗?我还没有找到像

prompt=select_account+consent
这样的配置。

现在,有了这些代码,用户注销后再次尝试登录,就会自动使用所选帐户登录,不会出现用户选择帐户的窗口。

pubspec.yaml

firebase_auth: ^0.8.1+4
google_sign_in: ^3.2.4

登录部分

GoogleSignInAccount googleUser = await _googleSignIn.signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;

final AuthCredential credential = GoogleAuthProvider.getCredential(
  accessToken: googleAuth.accessToken,
  idToken: googleAuth.idToken,
);
user = await _auth.signInWithCredential(credential);

注销部分

await FirebaseAuth.instance.signOut();
GoogleSignIn _googleSignIn = GoogleSignIn();
await _googleSignIn.signOut();
flutter firebase-authentication google-signin
3个回答
48
投票

注销前使用

GoogleSignInAccount.disconnect()
撤销之前的身份验证:

await _googleSignIn.disconnect();
await FirebaseAuth.instance.signOut();

4
投票

Harold 的答案曾经对我有用,但最近我测试的某些设备上的 GoogleSignIn().currentUser 显示为 null,然后断开连接功能将无法工作。因此,解决该问题的方法是确保它登录到 Google。

final googleCurrentUser =
        GoogleSignIn().currentUser ?? await GoogleSignIn().signIn();
    if (googleCurrentUser != null)
      await GoogleSignIn().disconnect().catchError((e, stack) {
        FirebaseCrashlytics.instance.recordError(e, stack);
      });
    await _auth.signOut();

-4
投票

一个简单的方法:- 在您的注销方法中,只需使用

_auth.signOut();

现在在 Google Sign In 包中,在 google_sign_in.dart 中

 Future<GoogleSignInAccount> signIn() {
    final Future<GoogleSignInAccount> result =
        _addMethodCall(GoogleSignInPlatform.instance.signIn, canSkipCall: false);
    bool isCanceled(dynamic error) =>
        error is PlatformException && error.code == kSignInCanceledError;
    return result.catchError((dynamic _) => null, test: isCanceled);
  }

找到上面的方法&将canSkipCall参数设置为false

final Future<GoogleSignInAccount> result =
        _addMethodCall(GoogleSignInPlatform.instance.signIn, canSkipCall: false);

这将允许您在每次尝试登录时选择用户

© www.soinside.com 2019 - 2024. All rights reserved.