Firebase for Flutter 中此身份验证背后的逻辑是什么?
What is the logic behind this authentication in Firebase for Flutter?
我正在关注 Firebase for Flutter Codelab and on the 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
包记录用户,然后不管我们会做什么在 user
中,我们尝试使用 FirebaseAuth
再次验证用户,这反过来也会使用 GoogleSignIn
.
你能解释一下为什么我们都这样做吗?
我的目标是为打开我的应用程序的用户提供两个单独的屏幕 - 一个用于 unauthorized/anonymous(将具有登录和注册选项),另一个用于将看到正常应用程序界面的授权用户。
该 Codelab 中的登录示例似乎写得不好,因为用户可能会取消 non-silent signIn()
然后当他们尝试访问 [=12] 时 googleSignIn.currentUser
将为 null =].我认为更好的处理方法是触发 Google 登录并在 googleSignIn.onAuthStateChanged
侦听器中处理 Firebase 身份验证。
至于为什么在该示例中同时使用两者: 如果您想使用 Google 帐户对 Firebase 上的用户进行身份验证,您必须提供 idToken
和 accessToken
,必须通过有效的 Google 登录获得。因此,首先,您让他们登录他们的 Google 帐户(通过 googleSignIn
)并使用其中的令牌通过 Firebase 进行身份验证(通过 auth
)。
仅当您想使用 Google 帐户通过 Firebase 进行身份验证时才需要使用 googleSignIn
;您还可以将 Firebase Auth 与 username/password 组合(这需要首先在 Firebase 上创建帐户)或来自 Facebook 登录的令牌或其他一些 OAuth 令牌一起使用。
我正在关注 Firebase for Flutter Codelab and on the 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
包记录用户,然后不管我们会做什么在 user
中,我们尝试使用 FirebaseAuth
再次验证用户,这反过来也会使用 GoogleSignIn
.
你能解释一下为什么我们都这样做吗? 我的目标是为打开我的应用程序的用户提供两个单独的屏幕 - 一个用于 unauthorized/anonymous(将具有登录和注册选项),另一个用于将看到正常应用程序界面的授权用户。
该 Codelab 中的登录示例似乎写得不好,因为用户可能会取消 non-silent signIn()
然后当他们尝试访问 [=12] 时 googleSignIn.currentUser
将为 null =].我认为更好的处理方法是触发 Google 登录并在 googleSignIn.onAuthStateChanged
侦听器中处理 Firebase 身份验证。
至于为什么在该示例中同时使用两者: 如果您想使用 Google 帐户对 Firebase 上的用户进行身份验证,您必须提供 idToken
和 accessToken
,必须通过有效的 Google 登录获得。因此,首先,您让他们登录他们的 Google 帐户(通过 googleSignIn
)并使用其中的令牌通过 Firebase 进行身份验证(通过 auth
)。
仅当您想使用 Google 帐户通过 Firebase 进行身份验证时才需要使用 googleSignIn
;您还可以将 Firebase Auth 与 username/password 组合(这需要首先在 Firebase 上创建帐户)或来自 Facebook 登录的令牌或其他一些 OAuth 令牌一起使用。