import React, { useEffect } from "react"; import { Link, useNavigate } from "react-router-dom"; import { CButton, CCard, CCardBody, CCardGroup, CCol, CContainer, CForm, CFormInput, CInputGroup, CInputGroupText, CRow, } from "@coreui/react"; import CIcon from "@coreui/icons-react"; import { cilLockLocked, cilUser } from "@coreui/icons"; import ClipLoader from "react-spinners/ClipLoader"; import { useState } from "react"; import axios from "axios"; import { useHistory } from "react-router-dom"; import swal from "sweetalert"; const Login = () => { const [loading, setLoading] = useState(false); const [validForm, setValidForm] = useState(false); const [auth, setAuth] = useState({ email: "", password: "", }); const [errors, setErrors] = useState({ emailError: "", passwordError: "", }); const validEmailRegex = RegExp( /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i ); const validPasswordRegex = RegExp( /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{7,}$/ ); const history = useNavigate(); // const handleChange = (e) => (event) => { // setAuth({ ...auth, [e]: event.target.value }); // }; const validateForm = () => { let valid = true; Object.values(errors).forEach((val) => { if (val.length > 0) { valid = false; return false; } }); Object.values(auth).forEach((val) => { if (val.length <= 0) { valid = false; return false; } }); return valid; }; //cheking email and password useEffect(() => { if (validateForm()) { setValidForm(true); } else { setValidForm(false); } }, [errors]); const handleChange = (e) => { const { name, value } = e.target; switch (name) { case "email": setErrors({ ...errors, emailError: validEmailRegex.test(value) ? "" : "Email is not valid!", }); break; case "password": setErrors((errors) => ({ ...errors, passwordError: validPasswordRegex.test(value) ? "" : "Password Shoud Be 8 Characters Long, Atleast One Uppercase, Atleast One Lowercase,Atleast One Digit, Atleast One Special Character", })); break; default: break; } setAuth({ ...auth, [name]: value }); }; const Login = async () => { if (!(auth.email && auth.password)) { return swal("Error!", "All fields are required", "error"); } setLoading({ loading: true }); try { const res = await axios.post("/api/v1/user/login/", auth); console.log(res); if (res.data.success == true) { localStorage.setItem("authToken", res.data.token); let response = await axios.get(`/api/v1/user/details`, { headers: { Authorization: `Bearer ${res.data.token}`, }, }); // console.log(response.data) const data = response.data; if (data.user.role === "admin" || data.user.role === "Employee") { history("/dashboard"); setLoading(false); window.location.reload(); } else { swal("Error!", "please try with admin credential!!", "error"); setLoading(false); } } else { setLoading(false); swal("Error!", "Invalid Credentials", "error"); } } catch (error) { setLoading(false); swal("Error!", "Invalid Credentials", "error"); } }; return (
Sign In to Your Cheminova Admin Dashboard.
{errors.emailError}
)}{errors.passwordError}
)}