You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using ky + react + store (mobx) to communicate with backend.
And I'm trying to figure out how to implement submit new access and refresh token pair back to store in case of getting 401 error.
Here is partial implementation of my task. It doesn't contains submitting new access and refresh token pair back to store:
export const api = (token: string) => {
return ky.extend({
headers: {
Authorization: token ? `Bearer ${token}` : "",
},
hooks: {
afterResponse: [
// retry with a fresh token on a 401 error
async (request, _options, response) => {
if (response.status === 401) {
// Get a fresh token
const {
data: {
accessToken: { token: newAccessToken },
refreshToken: { token: newRefreshToken },
},
}: IAPIItemResponse<IAPIToken> = await ky(
baseURL + "identity/token"
).json<IAPIItemResponse<IAPIToken>>();
// Retry with the token
request.headers.set("Authorization", `Bearer ${newAccessToken}`);
return await ky(request);
}
},
],
},
});
};
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm using ky + react + store (mobx) to communicate with backend.
And I'm trying to figure out how to implement submit new access and refresh token pair back to store in case of getting 401 error.
Here is partial implementation of my task. It doesn't contains submitting new access and refresh token pair back to store:
Is it possible to use ky to resolve this task?
Beta Was this translation helpful? Give feedback.
All reactions