import React, { useState, useRef, useEffect } from "react"; import { useLocation, useNavigate } from "react-router-dom"; import { toast } from "react-hot-toast"; import { AnimatePresence, motion } from "framer-motion"; import { useForm } from "react-hook-form"; import { useAuth } from "@web/src/providers/auth-provider"; import { RegisterForm } from "./register"; import { LoginForm } from "./login"; import { LoginFormInputs, RegisterFormInputs } from "./types"; import { Button } from "@web/src/components/common/element/Button"; const AuthPage: React.FC = () => { const [showLogin, setShowLogin] = useState(true); const [isLoading, setIsLoading] = useState(false); const { login, isAuthenticated, signup } = useAuth(); const location = useLocation(); const navigate = useNavigate(); const loginForm = useForm({ mode: "onChange" }); const registerForm = useForm({ mode: "onChange" }); const onSubmitLogin = async (data: LoginFormInputs) => { try { setIsLoading(true); console.log(data) await login(data.username, data.password); toast.success("Welcome back!"); } catch (err: any) { toast.error(err?.response?.data?.message || "Invalid credentials"); } finally { setIsLoading(false); } }; const onSubmitRegister = async (data: RegisterFormInputs) => { try { setIsLoading(true); await signup(data); toast.success("Registration successful!"); setShowLogin(true); } catch (err: any) { toast.error(err?.response?.data?.message); } finally { setIsLoading(false); } }; useEffect(() => { if (isAuthenticated) { const params = new URLSearchParams(location.search); const redirectUrl = params.get("redirect_url") || "/"; navigate(redirectUrl, { replace: true }); } }, [isAuthenticated, location]); return (
{/* Left Panel */} {/* Logo Section */}
United States Air Force Logo
{/* Title Section */}

USAF Leadership Portal

{showLogin ? "Access your secure USAF portal" : "Create your authorized account"}

{/* Switch Form Button */} {/* Right Panel - Forms */}
{showLogin ? ( ) : ( )}
); }; export default AuthPage;