import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { AlertCircle, CheckCircleIcon, XCircleIcon, Loader2, ShieldCheck, Search, Lightbulb } from "lucide-react" import { useState, useEffect } from "react" import { SiAuthentik } from "react-icons/si" import { type SecurityResults } from "@/app/api/users/security/route" interface CategoryChecks { passed: number failed: number total: number } export const SecurityTab = () => { const [scanning, setScanning] = useState(false) const [scanResults, setScanResults] = useState(null) const [error, setError] = useState(null) const [authentikChecks, setAuthentikChecks] = useState({ passed: 0, failed: 0, total: 0 }) const [overallChecks, setOverallChecks] = useState({ passed: 0, failed: 0, total: 0 }) const scanAcc = async () => { setScanning(true) try { const res = await fetch("/api/users/security") const data = await res.json() setScanResults(data) setScanning(false) } catch (error) { console.error(error) setError(error instanceof Error ? error.message : "An unknown error occurred") setScanning(false) } } // If password was changed over 3 months ago, this will be true const shouldResetPass = scanResults?.authentik?.passwordChangeDate && scanResults?.authentik?.passwordChangeDate < new Date(Date.now() - 90 * 24 * 60 * 60 * 1000) // If user has no 2FA methods setup, this will be true const insufficient2FA = scanResults?.authentik?.authenticators.length === 0 useEffect(() => { if (scanResults) { const authentik: CategoryChecks = { passed: 0, failed: 0, total: 0 } // Password age authentik.total++ if (!shouldResetPass) { authentik.passed++ } else { authentik.failed++ } // 2FA authentik.total++ if (!insufficient2FA) { authentik.passed++ } else { authentik.failed++ } setAuthentikChecks(authentik) setOverallChecks(authentik) } }, [scanResults, shouldResetPass, insufficient2FA]) const calculatePercentage = (passed: number, total: number) => { if (total === 0) return 0 return Math.round((passed / total) * 100) } return (
Account Security Scan Evaluate the security of your account with a simple button click! {error ? (

Error

{error}

) : scanResults ? ( <>

Authentik

{authentikChecks.passed}/{authentikChecks.total} checks passed
{shouldResetPass ? (

Password last changed {"on " + (scanResults?.authentik?.passwordChangeDate ? new Date(scanResults.authentik.passwordChangeDate).toLocaleDateString() : "never")}

) : (

Password last changed {"on " + (scanResults?.authentik?.passwordChangeDate ? new Date(scanResults.authentik.passwordChangeDate).toLocaleDateString() : "never")}

)} {insufficient2FA ? (

No 2FA methods setup

) : (

{scanResults?.authentik?.authenticators.length} two-factor authentication methods setup

)}
Results
{calculatePercentage(overallChecks.passed, overallChecks.total)}% {overallChecks.passed}/{overallChecks.total} checks passed
) : ( <> {scanning ? ( ) : ( )} )}
Recommendations Steps you can take to improve your account's security
  • Enable Two-Factor Authentication
  • Use a strong and unique password
  • Always make sure the URL matches librecloud.cc
  • https://librecloud.cc
    https://libre-cloud-login.com
) }