最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setContext not setting the token call before API call when token expires - Stack Overflow

programmeradmin3浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论