"use client"; import { useEffect, useState } from "react"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { createApiClient, formatAxiosError } from "@app/lib/api"; import { GenerateOidcUrlResponse } from "@server/routers/idp"; import { AxiosResponse } from "axios"; import { 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"; type AutoLoginHandlerProps = { resourceId: number; skipToIdpId: number; redirectUrl: string; }; export default function AutoLoginHandler({ resourceId, skipToIdpId, redirectUrl }: 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); try { const res = await api.post< AxiosResponse >(`/auth/idp/${skipToIdpId}/oidc/generate-url`, { redirectUrl }); if (res.data.data.redirectUrl) { // Redirect to the IDP for authentication window.location.href = res.data.data.redirectUrl; } else { setError(t("autoLoginErrorNoRedirectUrl")); } } catch (e) { console.error("Failed to generate OIDC URL:", e); setError(formatAxiosError(e, t("autoLoginErrorGeneratingUrl"))); } finally { setLoading(false); } } initiateAutoLogin(); }, []); return (
{t("autoLoginTitle")} {t("autoLoginDescription")} {loading && (
{t("autoLoginProcessing")}
)} {!loading && !error && (
{t("autoLoginRedirecting")}
)} {error && ( {t("autoLoginError")} {error} )}
); }