Flutter 身份验证从登录屏幕到登录屏幕使用 Google 登录和注销

phu*_*ing 4 firebase firebase-authentication google-signin flutter

今天我尝试构建 2 个屏幕“使用 GG 登录”和“注销重定向到登录屏幕”。成功!但是当我再次登录时,以前的 gg 帐户立即登录而不需要登录弹出窗口。在我的设备上记住登录的帐户。如何完全退出并使用其他帐户重新登录。这是我的代码:我有“使用谷歌登录的主页”和“带有注销按钮的主页中心页面”。此外,我有 api.dart 登录和注销,以及使用路由到 2 个页面的主页。- 主页:

routes: {
    "home-page": (context) => MyHomePage(),
    "game-center": (context) => GameCenterPage(),
  },
  home: MyHomePage(),
Run Code Online (Sandbox Code Playgroud)

- Api.dart:

class FBApi {
  static FirebaseAuth _auth = FirebaseAuth.instance;
  static GoogleSignIn _googleSignIn = GoogleSignIn();

  FirebaseUser firebaseUser;

  FBApi(FirebaseUser user) {
    this.firebaseUser = user;
  }

  static Future<FBApi> signInWithGoogle() async {
    final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
    final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

    final FirebaseUser user = await _auth.signInWithGoogle(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    assert(user.email != null);
    assert(user.displayName != null);

    assert(await user.getIdToken() != null);

    final FirebaseUser currentUser = await _auth.currentUser();
    assert(user.uid == currentUser.uid);

    return FBApi(user);
  }

  static Future<void> signOut() async {
    await _auth.signOut().then((_) {
      print("***** log out...what the hell?");
      // Navigator.of(context).pushNamedAndRemoveUntil("/login", 
ModalRoute.withName("/home"));
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

- 游戏中心.dart:

onPressed: () async {
          _signOut();
          Navigator.of(context).pushNamedAndRemoveUntil("home-page", ModalRoute.withName("game-center"));
        },
Run Code Online (Sandbox Code Playgroud)

- home.dart

class MyHomePage extends StatelessWidget {
  Future<bool> _loginUser() async {
    final api = await FBApi.signInWithGoogle();
    if (api != null) {
      return true;
    } else {
      return false;
    }
  }
  ...
Run Code Online (Sandbox Code Playgroud)

Cha*_*_oz 6

本周末我遇到了类似的问题,并通过退出 GoogleSignIn 解决了这个问题 - 但是我不确定这是否是正确的方法,因为我希望 FirbaseAuth 自动退出所有提供商。

static Future<void> signOut() async {
  await _auth.signOut().then((_) {

  //try the following
  _googleSignIn.signOut();
  //try the following

  Navigator.of(context).pushNamedAndRemoveUntil("/login", ModalRoute.withName("/home"));
});
Run Code Online (Sandbox Code Playgroud)