fix and update

This commit is contained in:
lendry
2026-06-29 21:36:32 +03:00
parent 8369abb023
commit 4cd75cb0b1
4 changed files with 65 additions and 18 deletions

View File

@@ -22,12 +22,14 @@ import {
PinVerificationResponse,
PublicUser,
refreshAuthSession,
resetApiGatewayWarmup,
resetPinRequiredNotification,
setPinRequiredHandler,
syncFedcmSession
} from '@/lib/api';
import { useToast } from './toast-provider';
import { PinLockModal } from './pin-lock-modal';
import { AppBootstrapScreen } from './app-bootstrap-screen';
import { EMBEDDED_AUTH_MESSAGE, type EmbeddedAuthPayload } from '@/lib/embedded-auth-bridge';
interface AuthContextValue {
@@ -112,6 +114,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
const [pinError, setPinError] = React.useState<string | null>(null);
const isPinLockedRef = React.useRef(false);
const refreshInFlightRef = React.useRef<Promise<void> | null>(null);
const initialBootstrapDoneRef = React.useRef(false);
React.useEffect(() => {
isPinLockedRef.current = isPinLocked;
@@ -160,8 +163,13 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
cacheUserProfile(auth.user);
setHasStoredSession(true);
clearPinLock();
initialBootstrapDoneRef.current = false;
resetApiGatewayWarmup();
setIsLoading(true);
void ensureApiGatewayReady().finally(() => setIsLoading(false));
void ensureApiGatewayReady().finally(() => {
initialBootstrapDoneRef.current = true;
setIsLoading(false);
});
if (auth.pinVerified !== false) {
void syncFedcmSession(auth.accessToken);
}
@@ -174,6 +182,8 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
window.localStorage.removeItem(AUTH_REFRESH_KEY);
window.localStorage.removeItem(AUTH_SESSION_KEY);
window.localStorage.removeItem(AUTH_USER_CACHE_KEY);
resetApiGatewayWarmup();
initialBootstrapDoneRef.current = false;
setToken(null);
setUser(null);
setHasStoredSession(false);
@@ -192,10 +202,17 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
setHasStoredSession(Boolean(refreshToken));
if (!currentToken && !refreshToken) {
initialBootstrapDoneRef.current = true;
setIsLoading(false);
return;
}
const isInitialBootstrap = !initialBootstrapDoneRef.current;
if (isInitialBootstrap) {
setIsLoading(true);
resetApiGatewayWarmup();
}
try {
if (currentToken) {
setToken(currentToken);
@@ -259,14 +276,17 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
const hasSession = Boolean(
window.localStorage.getItem(AUTH_TOKEN_KEY) || window.localStorage.getItem(AUTH_REFRESH_KEY)
);
if (hasSession) {
if (hasSession && isInitialBootstrap) {
try {
await ensureApiGatewayReady();
} catch {
// Продолжаем даже если gateway не ответил в срок прогрева.
}
}
setIsLoading(false);
if (isInitialBootstrap) {
initialBootstrapDoneRef.current = true;
setIsLoading(false);
}
refreshInFlightRef.current = null;
}
})();
@@ -519,6 +539,9 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
[login]
);
const isBootstrapping =
isLoading && !isPinLocked && Boolean(hasStoredSession || token || user);
return (
<AuthContext.Provider
value={{
@@ -542,7 +565,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
logout
}}
>
{children}
{isBootstrapping ? <AppBootstrapScreen /> : children}
<PinLockModal open={isPinLocked} isSubmitting={pinSubmitting} error={pinError} onSubmit={handlePinUnlock} />
</AuthContext.Provider>
);