import { useState, useEffect } from "react"; import { useLocation, useRevalidator } from "react-router"; import { useLocalAuthModalStore } from "@hooks/stores"; import { useDeviceUiNavigation } from "@hooks/useAppNavigation"; import { Button } from "@components/Button"; import { InputFieldWithLabel } from "@/components/InputField"; import api from "@/api"; import { m } from "@localizations/messages.js"; export default function SecurityAccessLocalAuthRoute() { const { setModalView } = useLocalAuthModalStore(); const { navigateTo } = useDeviceUiNavigation(); const location = useLocation(); const init = location.state?.init; useEffect(() => { if (!init) { navigateTo(".."); } else { setModalView(init); } }, [init, navigateTo, setModalView]); return navigateTo("..")} />; } export function Dialog({ onClose }: Readonly<{ onClose: () => void }>) { const { modalView, setModalView } = useLocalAuthModalStore(); const [error, setError] = useState(null); const revalidator = useRevalidator(); const handleCreatePassword = async (password: string, confirmPassword: string) => { if (password === "") { setError(m.local_auth_error_enter_password()); return; } if (password !== confirmPassword) { setError(m.local_auth_error_passwords_not_match()); return; } try { const res = await api.POST("/auth/password-local", { password }); if (res.ok) { setModalView("creationSuccess"); // The rest of the app needs to revalidate the device authMode revalidator.revalidate(); } else { const data = await res.json(); setError(data.error || m.local_auth_error_setting_password()); } } catch (error) { console.error(error); setError(m.local_auth_error_setting_password()); } }; const handleUpdatePassword = async ( oldPassword: string, newPassword: string, confirmNewPassword: string, ) => { if (newPassword !== confirmNewPassword) { setError(m.local_auth_error_passwords_not_match()); return; } if (oldPassword === "") { setError(m.local_auth_error_enter_old_password()); return; } if (newPassword === "") { setError(m.local_auth_error_enter_new_password()); return; } try { const res = await api.PUT("/auth/password-local", { oldPassword, newPassword, }); if (res.ok) { setModalView("updateSuccess"); // The rest of the app needs to revalidate the device authMode revalidator.revalidate(); } else { const data = await res.json(); setError(data.error || m.local_auth_error_changing_password()); } } catch (error) { console.error(error); setError(m.local_auth_error_changing_password()); } }; const handleDeletePassword = async (password: string) => { if (password === "") { setError(m.local_auth_error_enter_current_password()); return; } try { const res = await api.DELETE("/auth/local-password", { password }); if (res.ok) { setModalView("deleteSuccess"); // The rest of the app needs to revalidate the device authMode revalidator.revalidate(); } else { const data = await res.json(); setError(data.error || m.local_auth_error_disabling_password()); } } catch (error) { console.error(error); setError(m.local_auth_error_disabling_password()); } }; return (
{modalView === "createPassword" && ( )} {modalView === "deletePassword" && ( )} {modalView === "updatePassword" && ( )} {modalView === "creationSuccess" && ( )} {modalView === "deleteSuccess" && ( )} {modalView === "updateSuccess" && ( )}
); } function CreatePasswordModal({ onSetPassword, onCancel, error, }: { onSetPassword: (password: string, confirmPassword: string) => void; onCancel: () => void; error: string | null; }) { const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); return (
{ e.preventDefault(); }} >

{m.local_auth_create_title()}

{m.local_auth_create_description()}

setPassword(e.target.value)} /> setConfirmPassword(e.target.value)} />
{error &&

{error}

}
); } function DeletePasswordModal({ onDeletePassword, onCancel, error, }: { onDeletePassword: (password: string) => void; onCancel: () => void; error: string | null; }) { const [password, setPassword] = useState(""); return (

{m.local_auth_disable_local_device_protection_title()}

{m.local_auth_disable_local_device_protection_description()}

setPassword(e.target.value)} />
{error &&

{error}

}
); } function UpdatePasswordModal({ onUpdatePassword, onCancel, error, }: { onUpdatePassword: ( oldPassword: string, newPassword: string, confirmNewPassword: string, ) => void; onCancel: () => void; error: string | null; }) { const [oldPassword, setOldPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmNewPassword, setConfirmNewPassword] = useState(""); return (
{ e.preventDefault(); }} >

{m.local_auth_change_local_device_password_title()}

{m.local_auth_change_local_device_password_description()}

setOldPassword(e.target.value)} /> setNewPassword(e.target.value)} /> setConfirmNewPassword(e.target.value)} />
{error &&

{error}

}
); } function SuccessModal({ headline, description, onClose, }: { headline: string; description: string; onClose: () => void; }) { return (

{headline}

{description}

); }