fix: keep session when refresh fails on network error or 5xx (WealthLedger/frontend#97)
This commit was merged in pull request #100.
This commit is contained in:
@@ -67,6 +67,25 @@ describe("apiClient authentication", () => {
|
||||
expect(clear).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("keeps the session when refresh failed because the server was unreachable", async () => {
|
||||
const clear = vi.fn();
|
||||
server.use(
|
||||
http.get("*/api/v1/brokerage-accounts", () =>
|
||||
HttpResponse.json({ code: "UNAUTHORIZED" }, { status: 401 }),
|
||||
),
|
||||
);
|
||||
configureAuthClient({
|
||||
accessToken: () => "expired",
|
||||
clear,
|
||||
refresh: () => Promise.resolve(false),
|
||||
unavailable: () => true,
|
||||
});
|
||||
|
||||
await apiClient.GET("/api/v1/brokerage-accounts");
|
||||
|
||||
expect(clear).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not attach Bearer auth to authentication endpoints", async () => {
|
||||
let authorization: null | string
|
||||
|
||||
|
||||
@@ -32,6 +32,10 @@ type AuthHandlers = {
|
||||
clear: () => void;
|
||||
generation?: () => number;
|
||||
refresh: () => Promise<boolean>;
|
||||
/**
|
||||
True after a refresh failed transiently (network/5xx): the session is not proven gone.
|
||||
*/
|
||||
unavailable?: () => boolean;
|
||||
};
|
||||
|
||||
type ReadAttempt = {
|
||||
@@ -100,7 +104,8 @@ async function authenticatedFetch(request: Request): Promise<Response> {
|
||||
if (!isCurrent()) throw new DOMException("Session changed", "AbortError");
|
||||
|
||||
if (!(await sessionState.handlers.refresh())) {
|
||||
if (isCurrent()) sessionState.handlers.clear();
|
||||
if (isCurrent() && !sessionState.handlers.unavailable?.())
|
||||
sessionState.handlers.clear();
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -53,6 +53,9 @@ const ru = {
|
||||
passwordConfirmation: "Подтвердите пароль",
|
||||
register: "Регистрация",
|
||||
requestError: "Не удалось выполнить запрос. Попробуйте ещё раз.",
|
||||
retry: "Повторить",
|
||||
unavailableText: "Сервер временно недоступен. Ваш вход сохранён, попробуйте через минуту.",
|
||||
unavailableTitle: "Не удалось подключиться",
|
||||
validation: {
|
||||
invalidEmail: "Некорректный email",
|
||||
invalidPasswordLength: "Пароль: от 8 до 72 символов, только латинские буквы, цифры, спецсимволы и пробел",
|
||||
|
||||
Reference in New Issue
Block a user