reactjs - 我的 firebaseUser 在 apollo 客户端的 onError 函数中为空
问题描述
我的设置是带有 apollo 客户端和 firebase auth 的 nextJS。1小时后,我得到了graphqlError“auth/token-expired”,所以我想要执行firebase函数来获取新令牌,但我的用户在onError函数中为null。我究竟做错了什么?
const apolloClient = (param) => {
let { loggedIn } = nookies.get(param?.ctx, null)
const httpLink = createUploadLink({ uri: constants.gqlServer });
const errorLink = onError(({ response, operation, graphQLErrors, forward }) => {
if (graphQLErrors) {
for (let err of graphQLErrors) {
switch (err.extensions.code) {
case "auth/id-token-expired":
console.log(operation)
firebase.auth().onIdTokenChanged(user => {
if (user) {
user.getIdToken().then(function (accessToken) {
nookies.set(param.ctx, "loggedIn", accessToken, { path: "/" })
operation.setContext(({ headers = {} }) => ({
headers: {
// Re-add old headers
...headers,
// Switch out old access token for new one
authorization: `Bearer ${accessToken}`|| null
}
}));
forward(operation)
});
} else {
nookies.destroy(param.ctx, "loggedIn")
nookies.destroy(param.ctx, "uid")
nookies.destroy(param.ctx, "startupCheck")
handleLogOut()
// nookies.destroy(null, "url")
}
});
// nookies.destroy(param.ctx, "loggedIn")
// nookies.destroy(param.ctx, "uid")
// nookies.destroy(param.ctx, "startupCheck")
// response.errors = null;
}
}
}
})
const authLink = setContext(async (req, { headers }) => {
return {
headers: {
...headers,
authorization: loggedIn ? `Bearer ${loggedIn}` : null,
},
};
});
return new ApolloClient({
ssrMode: typeof window === "undefined",
link: ApolloLink.from([
errorLink,
authLink.concat(httpLink),
]),
credentials: "include",
cache: new InMemoryCache(),
})
}
解决方案
推荐阅读
- python - 基于 websockets 的 Python shell
- r - 逻辑回归中的总体检验显着性
- azure-devops - AzDO YAML 管道,在舞台上触发完成
- angular - Angular Firebase - html 视频下载了 2 次
- c - 如何从C中的另一个函数调用一个函数的值?
- python - 使用 numpy add.at 在整行上获取相同的值
- javascript - 重新加载时按钮颜色不会改变
- c++ - 为什么 CLion 可以运行我的程序,但我不能?[找不到程序入口点...]
- python - 如何在 python 脚本中读取 Firestore GeoPoint?
- mysql - 如何手动将csv导入mysql