[英]Firebase Auth not working in Flutter
不知何故 Firebase 身份驗證在我的實現中不起作用。 我在Firebase Codelab之后使用 Firebase 身份驗證實現了 Google 登錄,但將ensureLoggedIn
function 更改為:
Future<Null> _ensureLoggedIn() async {
GoogleSignInAccount user = googleSignIn.currentUser;
print('SIGNIN');
if (user == null) user = await googleSignIn.signInSilently();
if (user == null) {
user = await googleSignIn.signIn();
print('LOGIN');
}
if (auth.currentUser == null) {
GoogleSignInAuthentication credentials =
await googleSignIn.currentUser.authentication;
await auth.signInWithGoogle(
idToken: credentials.idToken,
accessToken: credentials.accessToken,
);
print('CURRENTUSER');
}
}
我使用控制台輸出來驗證 function 的哪些部分被執行。 每次啟動應用程序時都會調用 function,方法是在main()
function 中調用它
main() async{
new LinearProgressIndicator(backgroundColor: Colors.lightGreen,);
await _ensureLoggedIn();
runApp(new SpringsterApp());
}
然而,我注意到,盡管每次用戶尚未登錄時都會出現帶有可用 Google 帳戶的登錄提示,但ensureLoggedIn
function 的最后一部分( if (auth.currentUser == null)
)無論如何都不會執行用戶是否登錄。
從print('CURRENTUSER');
開始我就注意到了這一點永遠不會出現在控制台中,而且因為在 Firebase Auth 控制台中沒有創建新用戶。 有誰知道為什么會發生這種情況以及我該如何解決這個問題?
我的 Firestore 安全規則設置為僅身份驗證,因此只要發生此錯誤,就無法寫入或讀取任何數據。 提前致謝!
對於任何感興趣的人,我可以按計划登錄:
Future<Null> _ensureLoggedIn() async {
GoogleSignInAccount user = googleSignIn.currentUser;
if (user == null) user = await googleSignIn.signInSilently();
if (user == null) {
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(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await auth.currentUser();
assert(user.uid == currentUser.uid);
}
}
我已經初始化了 firebase 應用程序。 但仍然無法連接到 firebase 身份驗證。 所以我遵循 pub dev docs firebase_auth 。
在模擬器上運行。
bool shouldUseFirebaseEmulator = false; // before main()
if (shouldUseFirebaseEmulator) {
await FirebaseAuth.instance.useAuthEmulator('localhost', 9099);
} // inside main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.