fix and update

This commit is contained in:
lendry
2026-06-30 17:57:09 +03:00
parent 521de7ea00
commit 2ea790d21d
5 changed files with 68 additions and 46 deletions

View File

@@ -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(