Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {
- LoginContainer, FormContainer, Form,
- Box, Label, ConfirmationLabel, Input, Button
- } from '../styles/pages/login.style';
- import React, { useRef, useState } from 'react';
- import { useNavigate } from 'react-router-dom';
- import api from '../api';
- export default function Login() {
- const formRef = useRef();
- const navigate = useNavigate("");
- const [userNotFound, setUserNotFound] = useState(false);
- const errorMessage = userNotFound ? "Email/Password Does Not Exist!" : null;
- const handleFormSubmit = async (event) => {
- event.preventDefault();
- const form = formRef.current;
- const formData = new FormData(form);
- const emailAddress = formData.get("email_address");
- const password = formData.get("password");
- if (emailAddress.trim() === '' || password.trim() === '') {
- alert("All Entries Are Required!");
- return;
- }
- const postData = { emailAddress, password };
- try {
- await api.get("/login", postData);
- form.reset();
- navigate("/");
- } catch (error) {
- if (error.response.status === 409) {
- setUserNotFound(true);
- return;
- }
- else if (error.response.status === 400) {
- alert(error.response.data.detail);
- return;
- }
- else {
- console.log(error);
- return;
- }
- };
- };
- return (
- <LoginContainer className="container-fluid">
- <FormContainer className="container-fluid">
- <Form onSubmit={handleFormSubmit} ref={formRef}>
- <Box className="mb-3">
- <Label htmlFor="email_address" className="form-label">Email Address</Label>
- <Input type="text"
- className="form-control"
- id="email_address"
- name="email_address" />
- </Box>
- <Box className="mb-3">
- <Label htmlFor="password" className="form-label">Password</Label>
- <Input type="password"
- className="form-control"
- id="password"
- name="password" />
- </Box>
- {
- userNotFound &&
- <ConfirmationLabel>{ errorMessage }</ConfirmationLabel>
- }
- <Button type="submit" className="btn">Login</Button>
- </Form>
- </FormContainer>
- </LoginContainer>
- );
- };
Advertisement
Add Comment
Please, Sign In to add comment