Firebase auth user.getIdToken() sending expired tokens

前提是你 提交于 2020-12-13 06:39:04

问题


As far as I know, getIdToken() should by default send an unexpired token to the server, handling the token refreshing internally. However, I am getting many many errors from my server regarding expired tokens.

On my frontend, I created axios instance as follows:

const base = axios.create({
  baseURL: apiUrl,
});

base.interceptors.request.use(async (config) => {
  const token = await getAccessToken();
  config.headers.Authorization = `Bearer ${token}`;
  return config;
});

const getAccessToken = async () => {
  const user = firebase.auth().currentUser;
  if (user) {
    const token = await user.getIdToken();
    return token;
  }
  return null;
};

Then on the receiving server, I verify the token by doing:

const admin = require("firebase-admin");

const auths = req.headers.authorization.split(" ");
admin.auth().verifyIdToken(auths[1]).then(async token => {
  ...
});

The error I receive from the server is:

Error: Firebase ID token has expired. Get a fresh ID token from your client app and try again (auth/id-token-expired). See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.

If I change getIdToken() to getIdToken(true), the issue goes away.

I verified that the getAccessToken method is called for every request made. I'm a bit lost why my server continues to complain about expired tokens, any help would be appreciated.

来源:https://stackoverflow.com/questions/58968700/firebase-auth-user-getidtoken-sending-expired-tokens

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