fix and update

This commit is contained in:
lendry
2026-06-30 14:12:20 +03:00
parent 2a88c87e94
commit 879875508f
7 changed files with 45 additions and 25 deletions

View File

@@ -105,17 +105,32 @@ function applySessionState(
}
}
function readInitialAuthState() {
if (typeof window === 'undefined') {
return { token: null as string | null, hasStoredSession: false, cachedUser: null as PublicUser | null };
}
const token = window.localStorage.getItem(AUTH_TOKEN_KEY);
const refresh = window.localStorage.getItem(AUTH_REFRESH_KEY);
const cachedUser = readCachedUserProfile();
return {
token,
hasStoredSession: Boolean(refresh || token),
cachedUser
};
}
export function AuthProvider({ children }: { children: React.ReactNode }) {
const router = useRouter();
const pathname = usePathname();
const { showToast } = useToast();
const [token, setToken] = React.useState<string | null>(null);
const [user, setUser] = React.useState<PublicUser | null>(null);
const initialAuth = React.useMemo(() => readInitialAuthState(), []);
const [token, setToken] = React.useState<string | null>(initialAuth.token);
const [user, setUser] = React.useState<PublicUser | null>(initialAuth.cachedUser);
const [isLoading, setIsLoading] = React.useState(true);
const [isApiReady, setIsApiReady] = React.useState(false);
const [isSessionReady, setIsSessionReady] = React.useState(false);
const [isPinLocked, setIsPinLocked] = React.useState(false);
const [hasStoredSession, setHasStoredSession] = React.useState(false);
const [hasStoredSession, setHasStoredSession] = React.useState(initialAuth.hasStoredSession);
const [lockedSessionId, setLockedSessionId] = React.useState<string | null>(null);
const [pinSubmitting, setPinSubmitting] = React.useState(false);
const [pinError, setPinError] = React.useState<string | null>(null);
@@ -338,6 +353,8 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
const message = getApiErrorMessage(error, 'Не удалось загрузить профиль');
if (isGatewayUnavailableError(error) && isInitialBootstrap) {
setBootstrapError('Не удалось подключиться к серверу API. Подождите несколько секунд и нажмите «Повторить».');
setIsApiReady(false);
setIsSessionReady(false);
return;
}
if (message) showToast(message);
@@ -348,8 +365,6 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
} finally {
if (isInitialBootstrap) {
initialBootstrapDoneRef.current = true;
setIsApiReady(isApiGatewayReady());
setIsSessionReady(isApiGatewayReady());
setIsLoading(false);
}
}