-
Hello :) I'm developing a re-authorization after seeing the link above. How do I get to the login page in baseQuery? I'd appreciate it if you could reply! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Well... how would you do that navigation outside of RTK Query? |
Beta Was this translation helpful? Give feedback.
-
I know there should be an example code of course for a detailed answer, but it's similar to the code on the link, so I didn't attach it. I'll mark the point of time I want and attach the code! // create a new mutex
const mutex = new Mutex();
const baseQuery = fetchBaseQuery({
baseUrl: constants.API_BASE_URL,
prepareHeaders: (headers, { extra }) => {
const accessToken = extra.cookies?.[constants.ACCESS_TOKEN_NAME];
if (accessToken) {
headers.set('authorization', `Bearer ${accessToken}`);
}
return headers;
},
credentials: 'include',
});
const refreshFetchBase = async (args, api, extraOptions) => {
// wait until the mutex is available without locking it
await mutex.waitForUnlock();
let result = await baseQuery(args, api, extraOptions);
if (result.error?.status === 401) {
// checking whether the mutex is locked
if (!mutex.isLocked()) {
const release = await mutex.acquire();
try {
const refreshToken = api.extra?.cookies?.[constants.REFRESH_TOKEN_NAME];
if (refreshToken) {
const refreshResult = await baseQuery(
{
url: constants.REFRESH_TOKEN_URL,
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
refreshToken,
},
},
api,
extraOptions,
);
if (refreshResult.data) {
// Retry the initial query
result = await baseQuery(args, api, extraOptions);
} else {
// I want to know how to get it to the login page right away at this point.
}
}
} finally {
// release must be called once the mutex should be released again.
release();
}
} else {
// wait until the mutex is available without locking it
await mutex.waitForUnlock();
result = await baseQuery(args, api, extraOptions);
}
}
return result;
}; |
Beta Was this translation helpful? Give feedback.
My question was "in your non-Redux-Toolkit-Code, how would you do a redirect/navigation?"