Firebase:为什么我要两次获得Firebase ID令牌? [英] Firebase: Why am I getting Firebase ID Tokens twice?

查看:98
本文介绍了Firebase:为什么我要两次获得Firebase ID令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下情况下,我可以看到两个不同的令牌:

From the following situations, I could see two different tokens:

  • 完成注册后,我获得了第一个 Firebase ID令牌.
  • 完全退出后回到我的应用程序时,我会得到一个 JWT.
  • After completing the sign-up, I get the first Firebase ID Token.
  • I'm getting a new JWT when I come back to my app after completely signing out.
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.

Even though I've updated the user's default profile by calling the function below, it seems like ID token does not contain user's displayName and photoURL.

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.

Note: After the sign-out and successfully logged into the app, the token's length gets much longer than the previous token. Also, it does contain displayName and photoURL as well.

我还在此处发布了我的相关问题,似乎令牌导致了问题.

I've also posted my related issue here, and it seems like the token causes the problem.

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

Why am I getting a new token? And how can I resolve this issue?

推荐答案

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

To get a new ID token with the latest claims after you update the profile via updateProfile, you can simply force token refresh by calling: currentUser.getIdToken(true).

请注意,当令牌过期时,自然会在令牌刷新时,新令牌会随着配置文件的更改自动获取最新的声明.要立即获取此消息,您可以强制刷新.

Note, naturally on token refresh when the token is expired, the new token will automatically pick up the latest claims with the profile changes. To get this immediately, you can force refresh.

这篇关于Firebase:为什么我要两次获得Firebase ID令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆