Files
IdP/apps/docs/lib/use-one-tap-urls.ts
2026-07-01 17:00:55 +03:00

58 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client';
import { useEffect, useMemo, useState } from 'react';
import { fetchPublicSettingsClient } from '@/lib/api';
import { buildOneTapUrls, type OneTapUrls } from '@/lib/one-tap-examples';
import { resolveFrontendBase, resolveOAuthApiBase } from '@/lib/oauth-url';
export function useOneTapUrls() {
const [urls, setUrls] = useState<OneTapUrls | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
let cancelled = false;
void fetchPublicSettingsClient()
.then((settings) => {
if (cancelled) return;
const apiBase = resolveOAuthApiBase(settings, '');
const frontendBase = resolveFrontendBase(settings, '');
if (!apiBase || !frontendBase) {
setError('Укажите PUBLIC_API_URL и PUBLIC_FRONTEND_URL в настройках IdP — примеры подставят актуальные URL автоматически.');
setUrls(null);
return;
}
setUrls(buildOneTapUrls(apiBase, frontendBase, settings.PROJECT_NAME?.trim() || 'MVK ID'));
setError(null);
})
.catch(() => {
if (cancelled) return;
setError('Не удалось загрузить публичные настройки IdP.');
setUrls(null);
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => {
cancelled = true;
};
}, []);
return useMemo(
() => ({
urls,
loading,
error,
apiBase: urls?.apiBase ?? '',
frontendBase: urls?.frontendBase ?? '',
projectName: urls?.projectName ?? 'MVK ID'
}),
[error, loading, urls]
);
}