final logout related fixes and cookies and session

This commit is contained in:
IGNY8 VPS (Salman)
2025-12-16 19:16:50 +00:00
parent 1887f2a665
commit 84fd4bc11a
5 changed files with 175 additions and 13 deletions

View File

@@ -24,6 +24,12 @@ export default function SignInForm() {
const [error, setError] = useState("");
const [logoutReason, setLogoutReason] = useState<LogoutReason | null>(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();
@@ -52,9 +58,10 @@ export default function SignInForm() {
}
}, []);
const handleSubmit = async (e: React.FormEvent) => {
const handleSubmit = async (e: React.FormEvent, forceLogout = false) => {
e.preventDefault();
setError("");
setSessionConflict(null);
if (!email || !password) {
setError("Please enter both email and password");
@@ -62,11 +69,21 @@ export default function SignInForm() {
}
try {
await login(email, password, isChecked);
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;
@@ -74,6 +91,12 @@ export default function SignInForm() {
setError(err.message || "Login failed. Please check your credentials.");
}
};
const handleForceLogout = async (e: React.FormEvent) => {
e.preventDefault();
await handleSubmit(e, true);
};
return (
<div className="flex flex-col flex-1">
<div className="w-full max-w-md pt-10 mx-auto">
@@ -202,6 +225,46 @@ export default function SignInForm() {
</div>
)}
{/* Session Conflict Alert */}
{sessionConflict && (
<div className="p-4 bg-orange-50 border border-orange-200 rounded-lg dark:bg-orange-900/20 dark:border-orange-800">
<div className="flex items-center gap-2 mb-3">
<svg className="w-5 h-5 text-orange-600 dark:text-orange-400" fill="currentColor" viewBox="0 0 20 20">
<path fillRule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clipRule="evenodd" />
</svg>
<h4 className="font-semibold text-orange-800 dark:text-orange-300">
Active Session Detected
</h4>
</div>
<p className="text-sm text-orange-700 dark:text-orange-400 mb-2">
You have an active session for:
</p>
<p className="text-sm font-semibold text-orange-800 dark:text-orange-300 mb-3">
{sessionConflict.existingUser.email}
</p>
<p className="text-sm text-orange-700 dark:text-orange-400 mb-4">
You're trying to login as: <strong>{sessionConflict.requestedUser.email}</strong>
</p>
<div className="flex gap-3">
<button
type="button"
onClick={handleForceLogout}
disabled={loading}
className="flex-1 px-4 py-2 text-sm font-medium text-white bg-orange-600 rounded-lg hover:bg-orange-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
>
{loading ? 'Logging out...' : 'Logout Previous & Continue'}
</button>
<button
type="button"
onClick={() => setSessionConflict(null)}
className="flex-1 px-4 py-2 text-sm font-medium text-orange-700 bg-orange-100 rounded-lg hover:bg-orange-200 dark:bg-orange-900/40 dark:text-orange-300 dark:hover:bg-orange-900/60 transition-colors"
>
Cancel
</button>
</div>
</div>
)}
{error && (
<div className="p-3 text-sm text-red-600 bg-red-50 border border-red-200 rounded-lg dark:bg-red-900/20 dark:text-red-400 dark:border-red-800">
{error}