"use client"; /** * Session Info Card — P-3 * * Displays current session details and provides session management * controls (logout, clear sessions) within the Security settings tab. */ import { useState, useEffect } from "react"; import { Card, Button } from "@/shared/components"; import { useTranslations } from "next-intl"; interface SessionInfo { authenticated: boolean; loginTime: string | null; sessionAge: string; ipAddress: string; userAgent: string; } export default function SessionInfoCard() { const [session, setSession] = useState(null); const [loading, setLoading] = useState(true); const t = useTranslations("settings"); useEffect(() => { let cancelled = false; async function loadSession() { // Build session info from client-side data const loginTime = sessionStorage.getItem("omniroute_login_time"); const now = Date.now(); let sessionAge = t("unknown"); if (loginTime) { const elapsed = now - parseInt(loginTime, 10); const hours = Math.floor(elapsed / 3600000); const minutes = Math.floor((elapsed % 3600000) / 60000); sessionAge = hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`; } let authenticated = false; try { const res = await fetch("/api/auth/status", { method: "GET", cache: "no-store", }); if (res.ok) { const data = await res.json(); authenticated = data.authenticated === true; } } catch { // Keep unauthenticated fallback on network errors. } if (cancelled) return; setSession({ authenticated, loginTime: loginTime ? new Date(parseInt(loginTime, 10)).toLocaleString() : null, sessionAge, ipAddress: "—", // Server-side only userAgent: navigator.userAgent.split(" ").slice(-2).join(" ") || t("unknown"), }); setLoading(false); } loadSession(); return () => { cancelled = true; }; }, []); const handleLogout = async () => { try { await fetch("/api/auth/logout", { method: "POST" }); sessionStorage.removeItem("omniroute_login_time"); window.location.href = "/"; } catch { window.location.href = "/"; } }; const handleClearStorage = () => { if (confirm(t("clearLocalDataConfirm"))) { localStorage.clear(); sessionStorage.clear(); window.location.reload(); } }; if (loading) { return (
); } return (

{t("session")}

{t("status")}
{session?.loginTime && (
{t("loginTime")} {session.loginTime}
)}
{t("sessionAge")} {session?.sessionAge}
{t("browser")} {session?.userAgent}
{session?.authenticated && ( )}
); }