diff --git a/src/i18n/langs/en.json b/src/i18n/langs/en.json index 83a33593..3b4dbdc3 100644 --- a/src/i18n/langs/en.json +++ b/src/i18n/langs/en.json @@ -118,6 +118,9 @@ "unlock": { "pass_c": "Password C", "required": "Password C is required" + }, + "login": { + "required": "Password A is required" } } }, diff --git a/src/pages/Login/index.tsx b/src/pages/Login/index.tsx index b9deb566..852e0cb3 100644 --- a/src/pages/Login/index.tsx +++ b/src/pages/Login/index.tsx @@ -7,18 +7,24 @@ import { ApiError, checkError } from "@/utils/checkError"; import { instance } from "@/utils/interceptor"; import { Button } from "@nextui-org/button"; import { Spinner } from "@nextui-org/react"; +import { Input } from "@nextui-org/react"; import { AxiosError } from "axios"; import { FC, FormEvent, useContext, useEffect, useRef, useState } from "react"; +import type { SubmitHandler } from "react-hook-form"; +import { useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { useLocation, useNavigate } from "react-router-dom"; +interface IFormInputs { + passwordInput: string; +} + const Login: FC = () => { const { t } = useTranslation(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(""); const { isLoggedIn, setIsLoggedIn } = useContext(AppContext); const navigate = useNavigate(); - const passwordInput = useRef(null); const location = useLocation(); const from = @@ -26,6 +32,12 @@ const Login: FC = () => { const queryParams = new URLSearchParams(window.location.search); const back = queryParams.get("back"); + const { + register, + handleSubmit, + formState: { errors, isValid }, + } = useForm({ mode: "onChange" }); + useEffect(() => { if (isLoggedIn) { if (back) { @@ -38,15 +50,14 @@ const Login: FC = () => { } }, [navigate, from, isLoggedIn, back]); - const loginHandler = async (e: FormEvent) => { - e.preventDefault(); - + const loginHandler: SubmitHandler = async (data: { + passwordInput: string; + }) => { setError(""); setIsLoading(true); - const password = passwordInput.current?.value; await instance - .post("/system/login", { password }) + .post("/system/login", { password: data.passwordInput }) .then((resp) => { // access_token was used in v1.8 localStorage.setItem(ACCESS_TOKEN, resp.data.access_token || resp.data); @@ -80,20 +91,36 @@ const Login: FC = () => { <>
- - - +
+ +
{error && {error}}