import { useEffect, useState } from "react"; import { Link } from "react-router-dom"; import { AuthWrapper } from "./AuthWrapper"; import { useNavigate } from "react-router-dom"; import "./page-auth.css"; import AuthRepository from "../../repositories/AuthRepository"; import showToast from "../../services/toastService"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; const LoginPage = () => { const navigate = useNavigate(); const [loading, setLoading] = useState(false); const [hidepass, setHidepass] = useState(true); const [IsLoginWithOTP, setLoginWithOtp] = useState(false); const [IsTriedOTPThrough, setIsTriedOTPThrough] = useState(false); const now = Date.now(); const loginSchema = IsLoginWithOTP ? z.object({ username: z.string() .trim() .email({ message: "Valid email required" }), }) : z.object({ username: z.string() .trim() .email({ message: "Valid email required" }), password: z.string() .trim() .min(1, { message: "Password required" }), rememberMe: z.boolean(), }); const { register, handleSubmit, formState: { errors }, reset, getValues, } = useForm({ resolver: zodResolver(loginSchema), }); const onSubmit = async (data) => { setLoading(true); try { const username = data.username.trim(); const password = data.password?.trim(); if (!IsLoginWithOTP) { const userCredential = { username, password, }; const response = await AuthRepository.login(userCredential); localStorage.setItem("jwtToken", response.data.token); localStorage.setItem("refreshToken", response.data.refreshToken); setLoading(false); navigate("/dashboard"); } else { await AuthRepository.sendOTP({ email: username }); showToast("OTP has been sent to your email.", "success"); localStorage.setItem("otpUsername", username); localStorage.setItem("otpSentTime", now.toString()); navigate("/auth/login-otp"); } } catch (err) { showToast("Invalid username or password.", "error"); setLoading(false); } }; useEffect(() => { const otpSentTime = localStorage.getItem("otpSentTime"); if ( localStorage.getItem("otpUsername") && IsLoginWithOTP && now - Number(otpSentTime) < 10 * 60 * 1000 ) { navigate("/auth/login-otp"); } }, [IsLoginWithOTP]); return (

Welcome to PMS!

{IsLoginWithOTP ? "Enter your email to receive a one-time password (OTP)." : "Please sign-in to your account and start the adventure."}

{errors.username && (
{errors.username.message}
)}
{!IsLoginWithOTP && ( <>
{errors.password && (
{errors.password.message}
)}
Forgot Password?
)}
{!IsLoginWithOTP &&
OR
} {!IsLoginWithOTP && ( )}

New on our platform? {IsLoginWithOTP ? ( setLoginWithOtp(false)} > Login With Password ) : ( Request a Demo )}

); }; export default LoginPage;