fix and update

This commit is contained in:
lendry
2026-07-01 14:34:55 +03:00
parent 55deb5c152
commit 0c9b8e2629
6 changed files with 137 additions and 19 deletions

View File

@@ -83,6 +83,12 @@ export default function LoginPage() {
const router = useRouter();
const finishLoginRedirect = useCallback(() => {
if (typeof window !== 'undefined') {
const fedcmLoginApi = (navigator as Navigator & { login?: { setStatus?: (status: string) => void } }).login;
if (window.opener && typeof fedcmLoginApi?.setStatus === 'function') {
return;
}
}
const redirectAfterLogin =
typeof window !== 'undefined' ? new URLSearchParams(window.location.search).get('redirect') : null;
if (redirectAfterLogin && redirectAfterLogin.startsWith('/')) {

View File

@@ -33,6 +33,7 @@ import {
subscribeApiReady,
subscribeApiNotReady,
} from '@/lib/api';
import { finalizeFedcmLogin, shouldFinalizeFedcmLogin } from '@/lib/fedcm-login-bridge';
import { useToast } from './toast-provider';
import { PinLockModal } from './pin-lock-modal';
import { AppBootstrapScreen } from './app-bootstrap-screen';
@@ -254,7 +255,11 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
});
if (auth.pinVerified !== false) {
scheduleFedcmSessionSync(auth.accessToken, 8000);
if (typeof window !== 'undefined' && shouldFinalizeFedcmLogin(window.location.pathname)) {
void finalizeFedcmLogin(auth.accessToken);
} else {
scheduleFedcmSessionSync(auth.accessToken, 8000);
}
}
},
[activatePinLock, clearPinLock, showToast]

View File

@@ -0,0 +1,47 @@
import { syncFedcmSession } from '@/lib/api';
type FedcmLoginNavigator = Navigator & {
login?: {
setStatus: (status: 'logged-in' | 'logged-out') => void;
};
};
type FedcmIdentityProviderGlobal = {
close?: () => void;
};
declare global {
interface Window {
IdentityProvider?: FedcmIdentityProviderGlobal;
}
}
/** Завершение входа в popup FedCM: cookie + Set-Login для браузера. */
export async function finalizeFedcmLogin(accessToken: string) {
if (typeof window === 'undefined' || !accessToken) {
return;
}
await syncFedcmSession(accessToken);
const loginApi = (navigator as FedcmLoginNavigator).login;
if (loginApi?.setStatus) {
try {
loginApi.setStatus('logged-in');
} catch {
// FedCM API может быть недоступен вне popup-контекста
}
}
if (window.IdentityProvider?.close) {
try {
window.IdentityProvider.close();
} catch {
// закрытие необязательно — браузер сам закроет popup после setStatus
}
}
}
export function shouldFinalizeFedcmLogin(pathname: string) {
return pathname.startsWith('/auth/login');
}