【问题标题】:What is the logic behind this authentication in Firebase for Flutter?在 Firebase for Flutter 中这种身份验证背后的逻辑是什么?
【发布时间】:2018-09-02 12:58:54
【问题描述】:

我正在关注Firebase for Flutter Codelab,在8th step 上有这个_ensureLoggedIn() 函数:

final _googleSignIn = new GoogleSignIn();
final _auth = FirebaseAuth.instance;

Future<Null> _ensureLoggedIn() async {
  GoogleSignInAccount user = _googleSignIn.currentUser;
  if (user == null)
    user = await _googleSignIn.signInSilently();
  if (user == null) {
    await _googleSignIn.signIn();
    analytics.logLogin();                                    
  }

  if (await auth.currentUser() == null) {
    GoogleSignInAuthentication credentials =
        await _googleSignIn.currentUser.authentication;
    await auth.signInWithGoogle(
      idToken: credentials.idToken,
      accessToken: credentials.accessToken,
    );
  }
}

作为 Flutter 和 Firebase 框架的新手,我真的很难理解其背后的逻辑:首先我们尝试使用 GoogleSignIn 包记录用户,然后不管我们将在 @987654326 中拥有什么@ 我们尝试使用FirebaseAuth 再次对用户进行身份验证,这反过来也将使用GoogleSignIn

你能解释一下为什么我们两者都做吗? 我的目标是为打开我的应用程序的用户提供两个单独的屏幕 - 一个用于未授权/匿名(将具有登录和注册选项),另一个用于将看到正常应用程序界面的授权用户。

【问题讨论】:

    标签: firebase dart google-signin flutter


    【解决方案1】:

    该 Codelab 中的登录示例似乎写得不好,因为用户可能会取消非静默 signIn(),然后当他们尝试访问 googleSignIn.currentUser.authenticationgoogleSignIn.currentUser 将为空。我认为更好的处理方法是触发 Google 登录并在 googleSignIn.onAuthStateChanged 侦听器中处理 Firebase 身份验证。

    至于为什么在该示例中同时使用两者:如果您想使用 Google 帐户对 Firebase 上的用户进行身份验证,您必须提供 idTokenaccessToken,必须获得通过有效的 Google 登录。因此,首先,您让他们登录到他们的 Google 帐户(通过 googleSignIn)并使用其中的令牌向 Firebase 进行身份验证(通过 auth)。

    仅当您想使用 Google 帐户向 Firebase 进行身份验证时,才需要使用 googleSignIn;您还可以将 Firebase Auth 与用户名/密码组合(要求首先在 Firebase 上创建帐户)或来自 Facebook 登录的令牌或其他 OAuth 令牌一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-05
      • 2017-10-17
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2022-01-01
      • 2020-05-22
      相关资源
      最近更新 更多