"use client"; import { useEffect, useState } from "react"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { createApiClient, formatAxiosError } from "@app/lib/api"; import { AxiosResponse } from "axios"; import { redirect, useRouter } from "next/navigation"; import { Card, CardHeader, CardTitle, CardContent, CardDescription } from "@app/components/ui/card"; import { Alert, AlertDescription } from "@app/components/ui/alert"; import { Loader2, CheckCircle2, AlertCircle } from "lucide-react"; import { useTranslations } from "next-intl"; import { generateOidcUrlProxy } from "@app/actions/server"; type AutoLoginHandlerProps = { resourceId: number; skipToIdpId: number; redirectUrl: string; orgId?: string; }; export default function AutoLoginHandler({ resourceId, skipToIdpId, redirectUrl, orgId }: AutoLoginHandlerProps) { const { env } = useEnvContext(); const api = createApiClient({ env }); const router = useRouter(); const t = useTranslations(); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { async function initiateAutoLogin() { setLoading(true); let doRedirect: string | undefined; try { const response = await generateOidcUrlProxy( skipToIdpId, redirectUrl, orgId ); if (response.error) { setError(response.message); setLoading(false); return; } const data = response.data; const url = data?.redirectUrl; if (url) { doRedirect = url; } else { setError(t("autoLoginErrorNoRedirectUrl")); } } catch (e: any) { console.error("Failed to generate OIDC URL:", e); setError( t("autoLoginErrorGeneratingUrl", { defaultValue: "An unexpected error occurred. Please try again." }) ); } finally { setLoading(false); if (doRedirect) { redirect(doRedirect); } } } initiateAutoLogin(); }, []); return (
{t("autoLoginTitle")} {t("autoLoginDescription")} {loading && (
{t("autoLoginProcessing")}
)} {!loading && !error && (
{t("autoLoginRedirecting")}
)} {error && ( {t("autoLoginError")} {error} )}
); }