// import OAuthGoogle from "./OAuthGoogle"; import axios from "axios"; import { useState } from "react"; // import { FcGoogle } from "react-icons/fc"; import { Link, useNavigate } from "react-router-dom"; import OAuthGoogle from "./OAuthGoogle"; import { Bounce, toast } from "react-toastify"; // import Cookies from "js-cookie"; import { useDispatch } from "react-redux"; import { AppDispatch } from "../../store/store"; // import api from "../../config/api"; interface FormDataState { email: string; password: string; } function LoginForm() { const [formData, setForm] = useState({ email: "", password: "" }); const [cacheKey, setCacheKey] = useState(''); const [nextClick, setNextClick] = useState(false); const dispatch = useDispatch(); const navigate = useNavigate(); const handleNextClick = () => { // Validation Starts if (formData.email === "") { return; } // Validate Email Format const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(formData.email)) { return; } // Validation Ends // Checking user exists getting cacheKey axios.get(`${import.meta.env.VITE_BASE_URI}/api/auth/user?email=${formData.email}`, { headers: { "ngrok-skip-browser-warning": "69420" } }) .then((res) => { if (res.status === 200) { toast.dismiss(); setCacheKey(res.data); setNextClick(true); } }) .catch((err) => { toast.dismiss(); toast.error("User Does Not Exists", { position: "top-center", autoClose: 5000, hideProgressBar: false, closeOnClick: false, pauseOnHover: true, draggable: true, progress: undefined, theme: "light", transition: Bounce, }); }); } const handleFormLoginClick = () => { // Validation Starts if (formData.email === "" || formData.password === "") { return; } // Validate Email Format const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(formData.email)) { return; } // Validation Ends // Login API Call axios.post(`${import.meta.env.VITE_BASE_URI}/api/auth/login`, { cacheKey: cacheKey, password: formData.password }, { withCredentials: true, headers: { "ngrok-skip-browser-warning": "69420", "Access-Control-Allow-Origin": "*" } } ) .then((res) => { if (res.status === 200) { toast.dismiss(); // Cookies.set("authorization", res.headers.authorization, { // path: "/", // secure: true, // Required for HTTPS // sameSite: "Lax", // Required for cross-origin requests // }); // dispatch(updateUser({ ...res.data, authorized: true, accessToken: res.headers.authorization })); setTimeout(() => navigate('/'), 1000); // Redirect after 1 second toast.success("Login Successful", { position: "top-center", autoClose: 5000, hideProgressBar: false, closeOnClick: false, pauseOnHover: true, draggable: true, progress: undefined, theme: "light", transition: Bounce, }); } }) .catch((err) => { if (err.status === 401) { toast.dismiss(); toast.error("Credentials Not Match", { position: "top-center", autoClose: 5000, hideProgressBar: false, closeOnClick: false, pauseOnHover: true, draggable: true, progress: undefined, theme: "light", transition: Bounce, }); } else { toast.dismiss(); toast.error("Some Error Occurred", { position: "top-center", autoClose: 5000, hideProgressBar: false, closeOnClick: false, pauseOnHover: true, draggable: true, progress: undefined, theme: "light", transition: Bounce, }); } }); } return (

Login

{ e.preventDefault(); }}>
setForm({ ...formData, email: e.target.value })} />
{ e.preventDefault(); handleFormLoginClick(); }}>
setForm({ ...formData, password: e.target.value })} />

or login with social platforms

Create an Account. Register

) } export default LoginForm