admin管理员组文章数量:1240996
Token API should get called before the GraphQL API but it's being called at the same time
I am fetching the headers inside setContext Inside commonAuthHeaders I am calling the API to get refresh token
const authLink = setContext((_, { headers }) => {
return new Promise( async (resolve, reject) => {
// get the authentication headers
const updatedHeaders = await commonAuthHeaders(headers)
resolve ({
headers: updatedHeaders
}
)
});
});
export const client = new ApolloClient({
link: from([authLink, errorLink , httpLink]),
cache,
connectToDevTools: process.env.NODE_ENV !== "production",
defaultOptions: {
watchQuery: {
fetchPolicy: "network-only",
nextFetchPolicy: "network-only",
},
},
});
export async function commonAuthHeaders(headers) {
const supportedLoginModes = ["autodesk", "google"]
const logInMode = getCookie("__login_mode");
let access_token = getCookie("__bauhub_token");
const refresh_token = getCookie("__bauhub_refresh_token")
const lang = getCookie("__bauhub_lang");
const currentOrgId = getCookie("__current_anization");
const pluginType = getCookie("pluginType")
const newHeaders = {
...headers,
"Apollo-Require-Preflight": true,
};
if (lang) newHeaders["Accept-Language"] = lang;
if (supportedLoginModes.includes(logInMode)) {
if (logInMode === "autodesk" && !access_token && refresh_token) {
access_token = await refreshAutodeskAccessToken()
}
if (logInMode === "google" && !access_token) {
access_token = await getFirebaseToken()
}
if (access_token && currentOrgId) newHeadersanizationId = currentOrgId;
if (logInMode && access_token) newHeaders.logInMode = logInMode;
if (pluginType) newHeaders.pluginType = pluginType
newHeaders.authorization = access_token ? `Bearer ${access_token}` : "";
return newHeaders
} else {
return headers
}
}
First token API should get called and then after getting the token the next GraphQL call should get placed.
本文标签: javascriptsetContext not setting the token call before API call when token expiresStack Overflow
版权声明:本文标题:javascript - setContext not setting the token call before API call when token expires - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740072443a2223122.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论