fix and update

This commit is contained in:
lendry
2026-06-30 09:11:55 +03:00
parent f1d6a5167f
commit 250976ca08
5 changed files with 94 additions and 142 deletions

View File

@@ -11,15 +11,10 @@ import { Button } from '@/components/ui/button';
import {
approveOAuthAuthorization,
checkOAuthConsent,
fetchAuthSession,
fetchOAuthClientPublicInfo,
type OAuthConsentCheckResponse
} from '@/lib/api';
import {
parseAuthorizationRedirect,
parsePopupOAuthParams,
postOneTapResult
} from '@/lib/oauth-popup-bridge';
import { deliverOAuthPopupResult, parsePopupOAuthParams, postOneTapResult } from '@/lib/oauth-popup-bridge';
function buildOAuthQuery(searchParams: URLSearchParams) {
const params = new URLSearchParams();
@@ -37,7 +32,6 @@ function OAuthAuthorizeContent() {
const [submitting, setSubmitting] = useState(false);
const [checkingConsent, setCheckingConsent] = useState(false);
const [error, setError] = useState<string | null>(null);
const [sessionChecked, setSessionChecked] = useState(false);
const [consentInfo, setConsentInfo] = useState<OAuthConsentCheckResponse | null>(null);
const [clientName, setClientName] = useState<string | null>(null);
const autoApproveStartedRef = useRef(false);
@@ -55,20 +49,26 @@ function OAuthAuthorizeContent() {
const clientLabel = clientName ?? consentInfo?.client?.name ?? 'Приложение';
useEffect(() => {
if (!clientId || isLoading) return;
let cancelled = false;
void fetchOAuthClientPublicInfo(clientId)
.then((info) => {
if (!cancelled && info.name.trim()) {
setClientName(info.name.trim());
}
})
.catch(() => undefined);
return () => {
cancelled = true;
};
}, [clientId, isLoading]);
const approve = useCallback(async () => {
if (!token || !user || isPinLocked) return;
setSubmitting(true);
setError(null);
try {
const data = await approveOAuthAuthorization(oauthQuery, token);
if (!data.redirectUrl) {
throw new Error('Сервер не вернул redirect URL');
}
if (deliverOAuthPopupResult(popupContext, data.redirectUrl) === 'popup') {
return;
}
window.location.href = data.redirectUrl;
} catch (err) {
setError(err instanceof Error ? err.message : 'Ошибка OAuth авторизации');
autoApproveStartedRef.current = false;
} finally {
setSubmitting(false);
}
}, [isPinLocked, oauthQuery, popupContext, token, user]);
useEffect(() => {
if (!searchParams.has('userId')) return;
@@ -84,72 +84,24 @@ function OAuthAuthorizeContent() {
}, [clientId, isLoading, isPinLocked, redirectUri, returnUrl, router, token, user]);
useEffect(() => {
if (isLoading || isPinLocked || !token || !user) {
setSessionChecked(false);
return;
}
let cancelled = false;
void fetchAuthSession(token)
.then(() => {
if (!cancelled) setSessionChecked(true);
})
.catch(() => {
if (!cancelled) {
setSessionChecked(false);
router.replace(`/auth/login?redirect=${encodeURIComponent(returnUrl)}`);
}
});
return () => {
cancelled = true;
};
}, [isLoading, isPinLocked, returnUrl, router, token, user]);
const finishPopupFlow = useCallback(
(redirectUrl: string) => {
if (!popupContext) return false;
const parsed = parseAuthorizationRedirect(redirectUrl);
postOneTapResult(popupContext.popupOrigin, {
code: parsed.code ?? undefined,
state: parsed.state ?? undefined,
token: parsed.code ?? undefined,
error: parsed.error ?? undefined,
errorDescription: parsed.errorDescription ?? undefined
});
return true;
},
[popupContext]
);
const approve = useCallback(async () => {
if (!token || !user || isPinLocked) return;
setSubmitting(true);
setError(null);
try {
const data = await approveOAuthAuthorization(oauthQuery, token);
if (!data.redirectUrl) {
throw new Error('Сервер не вернул redirect URL');
}
if (finishPopupFlow(data.redirectUrl)) {
return;
}
window.location.href = data.redirectUrl;
} catch (err) {
setError(err instanceof Error ? err.message : 'Ошибка OAuth авторизации');
autoApproveStartedRef.current = false;
} finally {
setSubmitting(false);
}
}, [finishPopupFlow, isPinLocked, oauthQuery, token, user]);
useEffect(() => {
if (!token || !clientId || !sessionChecked || isPinLocked) return;
if (isLoading || !token || !clientId || isPinLocked) return;
let cancelled = false;
setCheckingConsent(true);
void checkOAuthConsent(oauthQuery, token)
.then((result) => {
setError(null);
void (async () => {
try {
try {
const info = await fetchOAuthClientPublicInfo(clientId);
if (!cancelled && info.name.trim()) {
setClientName(info.name.trim());
}
} catch {
// название приложения необязательно для consent
}
const result = await checkOAuthConsent(oauthQuery, token);
if (cancelled) return;
setConsentInfo(result);
if (result.client?.name?.trim()) {
@@ -159,28 +111,27 @@ function OAuthAuthorizeContent() {
autoApproveStartedRef.current = true;
void approve();
}
})
.catch((err) => {
} catch (err) {
if (!cancelled) {
setError(err instanceof Error ? err.message : 'Не удалось проверить согласие');
}
})
.finally(() => {
} finally {
if (!cancelled) setCheckingConsent(false);
});
}
})();
return () => {
cancelled = true;
};
}, [approve, clientId, isPinLocked, oauthQuery, sessionChecked, token]);
}, [approve, clientId, isLoading, isPinLocked, oauthQuery, token]);
const cancel = useCallback(() => {
if (popupContext) {
postOneTapResult(popupContext.popupOrigin, {
const delivered = postOneTapResult(popupContext.popupOrigin, {
error: 'access_denied',
errorDescription: 'Пользователь отклонил запрос'
});
return;
if (delivered) return;
}
if (!redirectUri) {
@@ -198,7 +149,7 @@ function OAuthAuthorizeContent() {
}
}, [popupContext, redirectUri, router, state]);
const authReady = Boolean(user && token && !isPinLocked && sessionChecked);
const authReady = Boolean(user && token && !isPinLocked && !isLoading);
const scopeItems = consentInfo?.requestedScopes ?? [];
if (isLoading || (clientId && redirectUri && !authReady && !isPinLocked)) {