import { useState, useEffect } from "react"; import { Link, useNavigate, useLocation } from "react-router-dom"; import { ChevronLeftIcon, EyeCloseIcon, EyeIcon } from "../../icons"; import Label from "../form/Label"; import Input from "../form/input/InputField"; import Checkbox from "../form/input/Checkbox"; import Button from "../ui/button/Button"; import { useAuthStore } from "../../store/authStore"; interface LogoutReason { code: string; message: string; path: string; context?: any; timestamp: string; source: string; } export default function SignInForm() { const [showPassword, setShowPassword] = useState(false); const [isChecked, setIsChecked] = useState(false); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [logoutReason, setLogoutReason] = useState(null); const [showLogoutDetails, setShowLogoutDetails] = useState(false); const [sessionConflict, setSessionConflict] = useState<{ message: string; existingUser: { email: string; username: string; id: number }; requestedUser: { email: string; username: string; id: number }; } | null>(null); const navigate = useNavigate(); const location = useLocation(); const { login, loading } = useAuthStore(); // Check for logout reason on component mount useEffect(() => { try { const storedReason = localStorage.getItem('logout_reason'); if (storedReason) { const reason = JSON.parse(storedReason); setLogoutReason(reason); // Check if we've already logged this (prevent StrictMode duplicates) const loggedKey = 'logout_reason_logged'; const alreadyLogged = sessionStorage.getItem(loggedKey); if (!alreadyLogged) { // Single consolidated log console.error('🚨 LOGOUT:', reason.code, '-', reason.message, 'on', reason.path); sessionStorage.setItem(loggedKey, 'true'); } } } catch (e) { console.error('Failed to read logout reason:', e); } }, []); const handleSubmit = async (e: React.FormEvent, forceLogout = false) => { e.preventDefault(); setError(""); setSessionConflict(null); if (!email || !password) { setError("Please enter both email and password"); return; } try { await login(email, password, isChecked, forceLogout); // Redirect to the page user was trying to access, or home const from = (location.state as any)?.from?.pathname || "/"; navigate(from, { replace: true }); } catch (err: any) { // Handle session conflict if (err?.type === 'SESSION_CONFLICT') { setSessionConflict({ message: err.message, existingUser: err.existingUser, requestedUser: err.requestedUser, }); return; } if (err?.code === 'PLAN_REQUIRED') { window.location.href = 'https://igny8.com/pricing'; return; } setError(err.message || "Login failed. Please check your credentials."); } }; const handleForceLogout = async (e: React.FormEvent) => { e.preventDefault(); await handleSubmit(e, true); }; return (
{/* Back link removed - users shouldn't go back to dashboard when not logged in */}

Sign In

Enter your email and password to sign in!

Or
{/* Logout Reason Display */} {logoutReason && (

Session Ended

{logoutReason.message}

{logoutReason.path && logoutReason.path !== '/signin' && (

Original page: {logoutReason.path}

)}
{/* Expandable Technical Details */} {showLogoutDetails && (

Technical Details:

Code: {logoutReason.code}
Source: {logoutReason.source}
Time: {new Date(logoutReason.timestamp).toLocaleString()}
{logoutReason.context && Object.keys(logoutReason.context).length > 0 && (
Context:
                                {JSON.stringify(logoutReason.context, null, 2)}
                              
)}
)}
)} {/* Session Conflict Alert */} {sessionConflict && (

Active Session Detected

You have an active session for:

{sessionConflict.existingUser.email}

You're trying to login as: {sessionConflict.requestedUser.email}

)} {error && (
{error}
)}
setEmail(e.target.value)} placeholder="info@gmail.com" required />
setPassword(e.target.value)} placeholder="Enter your password" required /> setShowPassword(!showPassword)} className="absolute z-30 -translate-y-1/2 cursor-pointer right-4 top-1/2" > {showPassword ? ( ) : ( )}
Keep me logged in
Forgot password?

Don't have an account? {""} Sign Up

); }