fix and update
This commit is contained in:
@@ -29,7 +29,6 @@ import {
|
||||
setPinRequiredHandler,
|
||||
subscribeApiReady,
|
||||
subscribeApiNotReady,
|
||||
waitForStableGateway,
|
||||
} from '@/lib/api';
|
||||
import { useToast } from './toast-provider';
|
||||
import { PinLockModal } from './pin-lock-modal';
|
||||
@@ -200,7 +199,6 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
setIsApiReady(false);
|
||||
|
||||
try {
|
||||
await waitForStableGateway(1);
|
||||
const session = await fetchAuthSession(accessToken);
|
||||
applySessionState(session, { setUser, setToken, activatePinLock, clearPinLock });
|
||||
initialBootstrapDoneRef.current = true;
|
||||
@@ -230,14 +228,32 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
cacheUserProfile(auth.user);
|
||||
setHasStoredSession(true);
|
||||
clearPinLock();
|
||||
setBootstrapError(null);
|
||||
initialBootstrapDoneRef.current = true;
|
||||
setIsApiReady(true);
|
||||
setIsSessionReady(true);
|
||||
setIsLoading(false);
|
||||
|
||||
await warmUpAndApplySession(auth.accessToken);
|
||||
void fetchAuthSession(auth.accessToken)
|
||||
.then((session) => {
|
||||
applySessionState(session, { setUser, setToken, activatePinLock, clearPinLock });
|
||||
})
|
||||
.catch((error) => {
|
||||
if (isPinRequiredError(error)) {
|
||||
activatePinLock(error.sessionId ?? auth.sessionId);
|
||||
return;
|
||||
}
|
||||
if (!isGatewayUnavailableError(error)) {
|
||||
const message = getApiErrorMessage(error, 'Не удалось обновить профиль после входа');
|
||||
if (message) showToast(message);
|
||||
}
|
||||
});
|
||||
|
||||
if (auth.pinVerified !== false) {
|
||||
scheduleFedcmSessionSync(auth.accessToken, 8000);
|
||||
}
|
||||
},
|
||||
[clearPinLock, warmUpAndApplySession]
|
||||
[activatePinLock, clearPinLock, showToast]
|
||||
);
|
||||
|
||||
const logout = React.useCallback(() => {
|
||||
@@ -286,10 +302,6 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
try {
|
||||
for (let bootstrapAttempt = 0; bootstrapAttempt < 4; bootstrapAttempt += 1) {
|
||||
try {
|
||||
if (isInitialBootstrap) {
|
||||
await waitForStableGateway(1);
|
||||
}
|
||||
|
||||
if (currentToken) {
|
||||
setToken(currentToken);
|
||||
try {
|
||||
@@ -580,13 +592,29 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
window.localStorage.setItem(AUTH_SESSION_KEY, sessionId);
|
||||
setToken(response.accessToken);
|
||||
|
||||
await warmUpAndApplySession(response.accessToken);
|
||||
try {
|
||||
await warmUpAndApplySession(response.accessToken);
|
||||
} catch (error) {
|
||||
if (!isGatewayUnavailableError(error)) {
|
||||
throw error;
|
||||
}
|
||||
const cachedUser = readCachedUserProfile();
|
||||
if (cachedUser) {
|
||||
setUser(cachedUser);
|
||||
}
|
||||
clearPinLock();
|
||||
setBootstrapError(null);
|
||||
initialBootstrapDoneRef.current = true;
|
||||
setIsApiReady(true);
|
||||
setIsSessionReady(true);
|
||||
setIsLoading(false);
|
||||
}
|
||||
|
||||
if (pathname.startsWith('/auth/')) {
|
||||
router.replace('/');
|
||||
}
|
||||
},
|
||||
[pathname, router, warmUpAndApplySession]
|
||||
[clearPinLock, pathname, router, warmUpAndApplySession]
|
||||
);
|
||||
|
||||
const handlePinUnlock = React.useCallback(
|
||||
|
||||
@@ -523,8 +523,6 @@ export async function refreshAuthSession(): Promise<RefreshSessionResponse> {
|
||||
throw new ApiError('Refresh token недоступен на сервере', 401, 'NO_REFRESH');
|
||||
}
|
||||
|
||||
await ensureApiGatewayReady();
|
||||
|
||||
const refreshToken = window.localStorage.getItem(AUTH_REFRESH_KEY);
|
||||
const sessionId = window.localStorage.getItem(AUTH_SESSION_KEY);
|
||||
if (!refreshToken) {
|
||||
@@ -682,7 +680,7 @@ export function buildSystemSettingPayload(setting: Pick<SystemSetting, 'key' | '
|
||||
}
|
||||
|
||||
const GATEWAY_RETRY_STATUS = new Set([502, 503, 504]);
|
||||
const GATEWAY_RETRY_ATTEMPTS = 4;
|
||||
const GATEWAY_RETRY_ATTEMPTS = 6;
|
||||
const GATEWAY_STABLE_PROBE_INTERVAL_MS = 800;
|
||||
const GATEWAY_STABLE_MAX_ROUNDS = 8;
|
||||
const API_MAX_CONCURRENT = 6;
|
||||
@@ -872,10 +870,6 @@ async function fetchWithGatewayRetry(url: string, init: RequestInit): Promise<Re
|
||||
}
|
||||
|
||||
export async function apiFetch<T>(path: string, options: RequestInit = {}, token?: string | null, allowRetry = true): Promise<T> {
|
||||
if (typeof window !== 'undefined' && !gatewayReadyResolved && !path.startsWith('/health')) {
|
||||
await ensureApiGatewayReady();
|
||||
}
|
||||
|
||||
await acquireApiSlot();
|
||||
|
||||
const resolvedToken = resolveAccessToken(token);
|
||||
|
||||
Reference in New Issue
Block a user