"use client"; import { Alert } from "@/components/alert"; import { getDeviceAuthorizationRequest } from "@/lib/server/oidc"; import { useTranslations } from "next-intl"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { BackButton } from "./back-button"; import { Button, ButtonVariants } from "./button"; import { TextInput } from "./input"; import { Spinner } from "./spinner"; import { Translated } from "./translated"; type Inputs = { userCode: string; }; export function DeviceCodeForm({ userCode }: { userCode?: string }) { const router = useRouter(); const { register, handleSubmit, formState } = useForm({ mode: "onChange", defaultValues: { userCode: userCode || "", }, }); const t = useTranslations("device"); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); async function submitCodeAndContinue(value: Inputs): Promise { setLoading(true); const response = await getDeviceAuthorizationRequest(value.userCode) .catch(() => { setError("Could not continue the request"); return; }) .finally(() => { setLoading(false); }); if (!response || !response.deviceAuthorizationRequest?.id) { setError("Could not continue the request"); return; } return router.push( `/device/consent?` + new URLSearchParams({ requestId: `device_${response.deviceAuthorizationRequest.id}`, user_code: value.userCode, }).toString(), ); } return ( <>
{error && (
{error}
)}
); }