Firebase:为什么我两次获得Firebase ID令牌?

问题描述 投票:0回答:1

Question

从以下情况中,我可以看到两个不同的标记:

  • 完成注册后,我会获得第一个Firebase ID令牌。
  • 当我完全退出后回到我的应用程序时,我正在获得一个新的JWT。

What I've found

const app = express();

app.use((req, res, next) => {
  if (!req.headers.authorization) 
    return res.status(403).json({ message: 'Missing Authorization Header' });

  const jwt = req.headers.authorization.trim();

  return admin.auth().verifyIdToken(jwt).then((decodedToken) => {
    const uid = decodedToken.uid; // Exists
    const displayName = decodedToken.name; // No displayName, object Undefined
    const photoURL = decodedToken.picture; // No photoURL, object Undefined
    next();
  });
});

即使我通过调用下面的函数更新了用户的默认配置文件,似乎ID令牌不包含用户的displayNamephotoURL

initializeUserProfile(name: string, photoURL: string) {
  let data = {
    displayName: name,
    photoURL: photoURL
  };

  this.afAuth.auth.currentUser.updateProfile(data).then(() => {
    this.getUsersRef(this.currentUserId).update(data); // Save data to firestore
  }).catch((err) => console.log(err));
}

注意:注销并成功登录应用程序后,令牌的长度比前一个令牌长得多。此外,它确实包含displayNamephotoURL

我还发布了我的相关问题here,似乎令牌导致问题。

我为什么要获得新令牌?我该如何解决这个问题?

javascript firebase firebase-authentication jwt
1个回答
0
投票

要在通过updateProfile更新配置文件后获取具有最新声明的新ID令牌,您只需通过调用:currentUser.getIdToken(true)强制令牌刷新。

请注意,当令牌过期时,令牌刷新时,新令牌会自动获取带有配置文件更改的最新声明。要立即获得此功能,您可以强制刷新。

© www.soinside.com 2019 - 2024. All rights reserved.