fix and update
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
||||
import { apiFetch, ensureApiGatewayReady } from '@/lib/api';
|
||||
import { apiFetch, ensureApiGatewayReady, isApiGatewayReady, subscribeApiReady } from '@/lib/api';
|
||||
|
||||
interface PublicSettingsContextValue {
|
||||
projectName: string;
|
||||
@@ -39,10 +39,13 @@ export function PublicSettingsProvider({ children }: { children: React.ReactNode
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
void refreshPublicSettings();
|
||||
const onFocus = () => void refreshPublicSettings();
|
||||
window.addEventListener('focus', onFocus);
|
||||
return () => window.removeEventListener('focus', onFocus);
|
||||
if (isApiGatewayReady()) {
|
||||
void refreshPublicSettings();
|
||||
return;
|
||||
}
|
||||
return subscribeApiReady(() => {
|
||||
void refreshPublicSettings();
|
||||
});
|
||||
}, [refreshPublicSettings]);
|
||||
|
||||
const value = useMemo(
|
||||
|
||||
Reference in New Issue
Block a user