import { Container, Image, Input, Text } from "@chakra-ui/react" import { Link as RouterLink, createFileRoute, redirect, } from "@tanstack/react-router" import { type SubmitHandler, useForm } from "react-hook-form" import { FiLock, FiMail } from "react-icons/fi" import Logo from "/assets/images/fastapi-logo.svg" import type { Body_login_login_access_token as AccessToken } from "../client" import { Button } from "../components/ui/button" import { Field } from "../components/ui/field" import { InputGroup } from "../components/ui/input-group" import { PasswordInput } from "../components/ui/password-input" import useAuth, { isLoggedIn } from "../hooks/useAuth" import { emailPattern, passwordRules } from "../utils" export const Route = createFileRoute("/login")({ component: Login, beforeLoad: async () => { if (isLoggedIn()) { throw redirect({ to: "/", }) } }, }) function Login() { const { loginMutation, error, resetError } = useAuth() const { register, handleSubmit, formState: { errors, isSubmitting }, } = useForm({ mode: "onBlur", criteriaMode: "all", defaultValues: { username: "", password: "", }, }) const onSubmit: SubmitHandler = async (data) => { if (isSubmitting) return resetError() try { await loginMutation.mutateAsync(data) } catch { // error is handled by useAuth hook } } return ( <> FastAPI logo }> } {...register("password", passwordRules())} placeholder="Password" errors={errors} /> Forgot Password? Don't have an account?{" "} Sign Up ) }