Two tokens with prepareHeaders #3718
-
Hello everyone, I have a question about the const baseQueryInitial = fetchBaseQuery({
baseUrl: mainBaseUrl,
prepareHeaders: async (headers, api) => {
const { getState, endpoint } = api;
const state = getState() as RootState;
if (endpointsWithB2CToken.includes(endpoint)) {
const appConfig = selectAppConfig(state);
const b2cToken: string = isEmptyObject(appConfig)
? ""
: await acquireAccessToken(appConfig);
headers.set("authorization", `Bearer ${b2cToken}`);
} else {
const internalToken = selectInternalToken(state);
headers.set("authorization", `Bearer ${internalToken}`);
}
return headers;
},
}); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
seems fine to me - i imagine this is the main reason endpoint is passed as part of the api object. |
Beta Was this translation helpful? Give feedback.
-
What do you think about using this approach? I will drill the property through headers, so I will put the type of token that should be used in the query, and then in the builder.query({
query: (customerId) => ({
url: '...url'
headers: {
tokenType: "first",
},
}),
});
prepareHeaders: async (headers) => {
const tokenType = headers.get("tokenType");
headers.delete("tokenType");
if (tokenType === 'first') {
headers.set("authorization", `Bearer ${firstToken}`);
} else {
headers.set("authorization", `Bearer ${secondToken}`);
}
return headers;
} |
Beta Was this translation helpful? Give feedback.
seems fine to me - i imagine this is the main reason endpoint is passed as part of the api object.