import { ViewIcon, ViewOffIcon } from "@chakra-ui/icons" import { Button, Center, Container, FormControl, FormErrorMessage, Icon, Image, Input, InputGroup, InputRightElement, Link, useBoolean, } from "@chakra-ui/react" import { Link as RouterLink, createFileRoute, redirect, } from "@tanstack/react-router" import { type SubmitHandler, useForm } from "react-hook-form" import Logo from "/assets/images/fastapi-logo.svg" import type { Body_login_login_access_token as AccessToken } from "../client" import useAuth, { isLoggedIn } from "../hooks/useAuth" import { emailPattern } from "../utils" export const Route = createFileRoute("/login")({ component: Login, beforeLoad: async () => { if (isLoggedIn()) { throw redirect({ to: "/", }) } }, }) function Login() { const [show, setShow] = useBoolean() 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 {errors.username && ( {errors.username.message} )} {show ? : } {error && {error}}
Forgot password?
) }