Node.js 从Firebase VerifyToken获取用户显示名称(第2部分)

Node.js 从Firebase VerifyToken获取用户显示名称(第2部分),node.js,firebase,firebase-authentication,firebase-admin,Node.js,Firebase,Firebase Authentication,Firebase Admin,我的处境和另一篇标题相同的文章一样。我有一个定制的后端(node+express),我能够更新前端的displayName。但是,在后端查看DecodedIdToken时,注册用户时没有“名称”。我需要用户的显示名称,以便它可以与后端上的其他客户端同步 如果我注销新注册的用户并重新登录,DecodedIdToken现在会在后端显示“名称” 客户端代码: firebase .auth() .createUserWithEmailAndPassword(email, password)

我的处境和另一篇标题相同的文章一样。我有一个定制的后端(node+express),我能够更新前端的displayName。但是,在后端查看DecodedIdToken时,注册用户时没有“名称”。我需要用户的显示名称,以便它可以与后端上的其他客户端同步

如果我注销新注册的用户并重新登录,DecodedIdToken现在会在后端显示“名称”

客户端代码:

firebase
  .auth()
  .createUserWithEmailAndPassword(email, password)
  .then(dataBeforeEmail => {
    firebase.auth().onAuthStateChanged(function(user) {
      user.sendEmailVerification();
      user.updateProfile({displayName: displayName})
    });
  })
 .then(dataAfterEmail => {
    firebase.auth().onAuthStateChanged(async function(user) {
      if (user) {
        // Sign up successful
        dispatch({
          type: REGISTER_SUCCESS,
          payload:user
        });

        const header = await tokenConfig();
       
        try{
          axios
          .post('/api/auth/',{}, header)
          .then(res=>
            dispatch({
              type: REGISTER_SUCCESS,
              payload: res.data
            })
            (console.log(res.data))
            )
        }
        catch(err) {
          dispatch({
            type: REGISTER_FAIL,
            payload:
                "Something went wrong, we couldn't create your account. Please try again."
            });
          };

export const tokenConfig = async () => {
  const user = firebase.auth().currentUser;
  const token = user && (await user.getIdToken());
 const config = {
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${token}`,
    },
  };
  return config;
};

有没有一种方法可以让它在不让用户注销和重新登录的情况下显示出来?

这是因为客户端SDK是如何处理ID令牌的。ID令牌最多可缓存一小时。因此,在ID令牌上反映对用户帐户的任何更改之前,必须执行以下操作之一:

  • 等待当前ID令牌过期,SDK自动获取新的ID令牌
  • 注销和登录,循环退出ID令牌
  • 在客户端SDK上显式请求ID令牌刷新。如果您使用的是JS SDK,则如下所示:
  • 有关详细信息:

    太棒了,(3)工作起来很有魅力。谢谢!
    firebase.auth().currentUser.getIdToken(/* forceRefresh */ true)