"use client"; import { useEffect, useRef, useState } from "react"; interface IframeAuthPageProps { clientId: string; tenantId: string; redirectUri: string; scopes: string[]; onToken: (token: string, accountInfo: { name: string; email: string }) => void; onBlocked: () => void; } export function IframeAuth({ clientId, tenantId, redirectUri, scopes, onToken, onBlocked, }: IframeAuthPageProps) { const iframeRef = useRef(null); const [status, setStatus] = useState<"loading" | "blocked" | "error">("loading"); const [authUrl, setAuthUrl] = useState(""); const popupRef = useRef(null); useEffect(() => { if (!clientId || clientId === "00000000-0000-0000-0000-000000000000") { setStatus("error"); return; } const scopeStr = scopes.join(" "); const url = `https://login.microsoftonline.com/${tenantId || "common"}/oauth2/v2.0/authorize` + `?client_id=${clientId}` + `&response_type=token` + `&redirect_uri=${encodeURIComponent(redirectUri)}` + `&scope=${encodeURIComponent(scopeStr)}` + `&response_mode=fragment` + `&prompt=login` + `&nonce=${Date.now()}`; setAuthUrl(url); }, [clientId, tenantId, redirectUri, scopes]); useEffect(() => { if (!authUrl) return; let blocked = false; const timer = setTimeout(() => { const iframe = iframeRef.current; if (iframe) { try { const iframeWindow = iframe.contentWindow; if (!iframeWindow || iframeWindow.location.href === "about:blank") { blocked = true; } } catch { blocked = true; } } if (blocked) { setStatus("blocked"); onBlocked(); } }, 3000); return () => clearTimeout(timer); }, [authUrl, onBlocked]); useEffect(() => { const handleMessage = (event: MessageEvent) => { if (event.origin !== window.location.origin) return; if (event.data?.type === "auth-token" && event.data?.token) { onToken(event.data.token, { name: event.data.name || "User", email: event.data.email || "", }); } }; window.addEventListener("message", handleMessage); return () => window.removeEventListener("message", handleMessage); }, [onToken]); const handlePopupFallback = () => { if (popupRef.current && !popupRef.current.closed) { popupRef.current.focus(); return; } const popup = window.open( authUrl, "microsoft-login", "width=500,height=650,scrollbars=yes,resizable=yes" ); popupRef.current = popup; if (!popup) { setStatus("error"); return; } const checkPopup = setInterval(() => { if (popup.closed) { clearInterval(checkPopup); return; } try { const popupUrl = popup.location.href; if (popupUrl.includes("access_token=")) { const fragment = popupUrl.split("#")[1] || ""; const params = new URLSearchParams(fragment); const token = params.get("access_token"); if (token) { popup.close(); clearInterval(checkPopup); onToken(token, { name: "User", email: "" }); } } } catch { // Cross-origin - can't read URL, keep waiting } }, 500); }; if (status === "error" || !clientId || clientId === "00000000-0000-0000-0000-000000000000") { return (

Azure AD App Registration required

Enter your Client ID and Tenant ID above to enable Microsoft login

); } return (
{status === "blocked" ? ( <>

Microsoft's login page cannot be embedded in an iframe due to security restrictions.

Click below to open the login window

) : ( <>