Firebase中的displayName为空的问题在使用signInWithCredential进行登录时出现
P粉328911308
P粉328911308 2023-09-04 09:43:01
[JavaScript讨论组]

我正在使用Google登录,提示用户输入Google账户的电子邮件和密码:

const auth = getAuth(app);
  const userCredentials = await signInWithCredential(
    auth,
    GoogleAuthProvider.credential(null, token)
  );
console.log(userCredentials.user)

我成功获取到用户对象,其中包括emailphotoURLuid等信息,但是displayName为空。这是一个错误还是预期结果?我尝试创建了一个新的Gmail账户,包括姓名和昵称,但是displayName仍然为空。我该如何获取displayName

P粉328911308
P粉328911308

全部回复(1)
P粉794851975

我找到了一个方法来“黑掉”关于无法从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中你可以获取显示名称。 希望这能帮助到某人!

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号