diff --git a/src/pages/admin/login.tsx b/src/pages/admin/login.tsx index a66a9b8..d5675e3 100644 --- a/src/pages/admin/login.tsx +++ b/src/pages/admin/login.tsx @@ -1,26 +1,80 @@ import { ArrowUpRightIcon } from "@heroicons/react/24/solid"; +import { signIn } from "next-auth/react"; +import { useCallback, useState } from "react"; +import { type SubmitHandler, useForm, type FieldError } from "react-hook-form"; import Footer from "~/components/Footer"; import Header from "~/components/Header"; import { TopLabel } from "~/components/Labels"; +import { type LoginInput } from "~/lib/validation/auth"; + +const FormFieldError = ({ error }: { error?: FieldError }) => { + return ( + {error?.message ?? ""} + ); +}; const SignInForm = () => { + const { + register, + handleSubmit, + formState: { errors }, + } = useForm({}); + + const [error, setError] = useState(undefined); + + /** + * Form submit handler. + * @param data Form data. + * @returns + */ + const onSubmit: SubmitHandler = useCallback(async (data) => { + try { + const res = await signIn("credentials", { + ...data, + redirect: false, + callbackUrl: "/", + }); + // error response + if (res?.error) setError("Invalid username or password"); + + // success response + if (res?.ok) window.location.replace("/resources"); + } catch (err) { + console.error(err); + } + }, []); + return ( Admin Login - + {/** Username section */} Username: - + + {/** Password section */} Password: - + + @@ -31,6 +85,9 @@ const SignInForm = () => { sign in + + {error ? {error} : undefined} +
{error}