|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import { ViewIcon, ViewOffIcon } from "@chakra-ui/icons"; |
| 4 | +import { Button, Center, Container, FormControl, Icon, Image, Input, InputGroup, InputRightElement, Link, useBoolean } from "@chakra-ui/react"; |
| 5 | +import { SubmitHandler, useForm } from "react-hook-form"; |
| 6 | +import { Link as ReactRouterLink, useNavigate } from "react-router-dom"; |
| 7 | + |
| 8 | +import Logo from "../../assets/images/fastapi-logo.png"; |
| 9 | +import { LoginService } from "../../client"; |
| 10 | +import { Body_login_login_access_token as AccessToken } from "../../client/models/Body_login_login_access_token"; |
| 11 | + |
| 12 | +const Login: React.FC = () => { |
| 13 | + const [show, setShow] = useBoolean(); |
| 14 | + const navigate = useNavigate(); |
| 15 | + const { register, handleSubmit } = useForm<AccessToken>(); |
| 16 | + |
| 17 | + const onSubmit: SubmitHandler<AccessToken> = async (data) => { |
| 18 | + const response = await LoginService.loginAccessToken({ |
| 19 | + formData: data, |
| 20 | + }); |
| 21 | + localStorage.setItem("access_token", response.access_token); |
| 22 | + navigate("/"); |
| 23 | + }; |
| 24 | + |
| 25 | + return ( |
| 26 | + <Container |
| 27 | + as="form" |
| 28 | + onSubmit={handleSubmit(onSubmit)} |
| 29 | + h="100vh" |
| 30 | + maxW="sm" |
| 31 | + alignItems="stretch" |
| 32 | + justifyContent="center" |
| 33 | + gap={4} |
| 34 | + centerContent |
| 35 | + > |
| 36 | + <Image src={Logo} alt="FastAPI logo" height="auto" maxW="2xs" alignSelf="center" /> |
| 37 | + <FormControl id="email"> |
| 38 | + <Input {...register("username")} focusBorderColor="blue.200" placeholder="Email" type="text" /> |
| 39 | + </FormControl> |
| 40 | + <FormControl id="password"> |
| 41 | + <InputGroup> |
| 42 | + <Input |
| 43 | + {...register("password")} |
| 44 | + type={show ? "text" : "password"} |
| 45 | + focusBorderColor="blue.200" |
| 46 | + placeholder="Password" |
| 47 | + /> |
| 48 | + <InputRightElement |
| 49 | + color="gray.500" |
| 50 | + _hover={{ |
| 51 | + cursor: "pointer", |
| 52 | + }} |
| 53 | + > |
| 54 | + <Icon onClick={setShow.toggle} aria-label={show ? "Hide password" : "Show password"}> |
| 55 | + {show ? <ViewOffIcon /> : <ViewIcon />} |
| 56 | + </Icon> |
| 57 | + </InputRightElement> |
| 58 | + </InputGroup> |
| 59 | + <Center> |
| 60 | + <Link as={ReactRouterLink} to="/recover-password" color="blue.500" mt={2}> |
| 61 | + Forgot password? |
| 62 | + </Link> |
| 63 | + </Center> |
| 64 | + </FormControl> |
| 65 | + <Button colorScheme="teal" type="submit"> |
| 66 | + Log In |
| 67 | + </Button> |
| 68 | + </Container> |
| 69 | + ); |
| 70 | +}; |
| 71 | + |
| 72 | +export default Login; |
0 commit comments