我正在使用Google登录,提示用户输入Google账户的电子邮件和密码:
const auth = getAuth(app);
const userCredentials = await signInWithCredential(
auth,
GoogleAuthProvider.credential(null, token)
);
console.log(userCredentials.user)
我成功获取到用户对象,其中包括email、photoURL、uid等信息,但是displayName为空。这是一个错误还是预期结果?我尝试创建了一个新的Gmail账户,包括姓名和昵称,但是displayName仍然为空。我该如何获取displayName?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我找到了一个方法来“黑掉”关于无法从
signInWithCredential获取displayName的问题。显然,正如之前所说的,displayName始终为null。但是,如果你使用google_sign_in库进行登录并检查GoogleSignInAccount对象,你将会看到你将拥有该显示名称。然后,你可以使用之前使用的Google登录凭据登录到Firebase。 以下是我使用的代码:try { GoogleSignIn _googleSignIn = GoogleSignIn(); GoogleSignInAccount? googleSignInAccount = await _googleSignIn.signIn(); if (googleSignInAccount == null) { print('error getting google sign in account'); return; } GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount!.authentication; AuthCredential credential = GoogleAuthProvider.credential( idToken: googleSignInAuthentication.idToken, accessToken: googleSignInAuthentication.accessToken); var result = (await _auth.signInWithCredential(credential)); user = result.user; displayName = googleSignInAccount.displayName; } catch (e) { print(e); }在
user变量中,你将拥有来自firebase身份验证的所有与用户相关的变量,例如uid,而从googleSignInAccount中你可以获取显示名称。 希望这能帮助到某人!