Firebase: Why am I getting Firebase ID Tokens twice?

扶醉桌前 提交于 2019-12-11 17:25:45

问题


Question

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

  • 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.

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();
  });
});

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));
}

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?


回答1:


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.



来源:https://stackoverflow.com/questions/48606298/firebase-why-am-i-getting-firebase-id-tokens-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!