gpt4 book ai didi

firebase - Firebase for Flutter 中此身份验证背后的逻辑是什么?

转载 作者:IT王子 更新时间:2023-10-29 06:34:05 32 4
gpt4 key购买 nike

我正在关注 Firebase for Flutter Codelab8th 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 包记录用户,然后不管我们做什么我们将在 user 中尝试再次使用 FirebaseAuth 对用户进行身份验证,这反过来也会使用 GoogleSignIn

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

最佳答案

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

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

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

关于firebase - Firebase for Flutter 中此身份验证背后的逻辑是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49456613/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com