import React, { useState } from "react"; import { Alert, Box, Button, CircularProgress, Paper, Typography } from "@mui/material"; import { useNavigate } from "react-router-dom"; import { api, getApiErrorMessage } from "../api"; import { getMicrosoftMsalInstance } from "../components/MicrosoftAuthCard"; import { useI18n } from "../i18n/I18nProvider"; export default function MicrosoftLegacyRelinkPage() { const { t } = useI18n(); const navigate = useNavigate(); const [working, setWorking] = useState(false); const [success, setSuccess] = useState(false); const [error, setError] = useState(null); const params = new URLSearchParams(window.location.search); const userId = params.get("userId") || ""; const tenantId = params.get("tenantId") || ""; const objectId = params.get("objectId") || ""; const recoveryToken = params.get("token") || ""; const clientId = (process.env.NEXT_PUBLIC_MICROSOFT_CLIENT_ID || "").trim(); const missing = !userId || !tenantId || !objectId || !recoveryToken || !clientId; async function confirm() { setWorking(true); setError(null); try { const msal = getMicrosoftMsalInstance(clientId); await msal.initialize(); const result = await msal.loginPopup({ scopes: ["openid", "profile", "email"] }); if (!result.idToken) throw new Error(t("microsoftAuthFailed")); await api.post("/auth/microsoft/legacy-relink/confirm", { userId, tenantId, objectId, recoveryToken, microsoftToken: result.idToken, }); setSuccess(true); } catch (e: any) { setError(getApiErrorMessage(e, t("microsoftLegacyRelinkFailed"))); } finally { setWorking(false); } } return ( {t("microsoftLegacyRelinkTitle")} {t("microsoftLegacyRelinkBody")} {missing ? {t("microsoftLegacyRelinkMissing")} : null} {error ? {error} : null} {success ? {t("microsoftLegacyRelinkSuccess")} : null} {!success ? ( ) : null} ); }