import type { Metadata } from 'next'; import { and, desc, eq, gt, isNull } from '@/lib/db'; import { db, loginEvents, sessions, trustedDevices } from '@/lib/db'; import { currentSessionId, requireUser } from '@/lib/auth/session'; import { deviceLabel } from '@/lib/auth/request'; import { revokeDeviceAction, revokeSessionAction, signOutEverywhereAction, toggleAlwaysAskCodeAction } from '@/lib/auth/account-actions'; import { PageHeader, btnDanger, btnSecondary } from '@/components/account/page-header'; import { Card, CardHeader, Badge, Table, th, td } from '@/components/ui/primitives'; import { fmtRelative } from '@/lib/format'; import { ChangePasswordForm, MfaSection } from './forms'; export const metadata: Metadata = { title: 'Security', robots: { index: false } }; export default async function SecurityPage() { const u = await requireUser('/account/security'); const current = await currentSessionId(); const [sess, devices, logins] = await Promise.all([ db().select().from(sessions).where(and(eq(sessions.userId, u.id), isNull(sessions.revokedAt), gt(sessions.expiresAt, new Date()))).orderBy(desc(sessions.lastSeenAt)), db().select().from(trustedDevices).where(and(eq(trustedDevices.userId, u.id), isNull(trustedDevices.revokedAt), gt(trustedDevices.expiresAt, new Date()))).orderBy(desc(trustedDevices.lastUsedAt)), db().select().from(loginEvents).where(eq(loginEvents.userId, u.id)).orderBy(desc(loginEvents.createdAt)).limit(25), ]); return ( <>
{u.mfaEnabled ? 'Authenticator on' : 'E-mail codes'}} />
} />
    {sess.map((s) => (
  • {deviceLabel(s.userAgent)} {s.id === current ? This device : null}

    {s.ip ?? 'unknown IP'} · active {fmtRelative(s.lastSeenAt ?? s.createdAt)} · expires {fmtRelative(s.expiresAt)}

    {s.id !== current ? (
    ) : null}
  • ))}
{devices.length === 0 ? (

No trusted devices.

) : (
    {devices.map((d) => (
  • {d.label ?? deviceLabel(d.userAgent)}

    {d.ip ?? 'unknown IP'} · last used {fmtRelative(d.lastUsedAt ?? d.createdAt)} · until {d.expiresAt.toISOString().slice(0, 10)}

  • ))}
)}
{logins.map((l) => ( ))} {logins.length === 0 ? ( ) : null}
When Outcome Method Device IP
{fmtRelative(l.createdAt)} {l.outcome.replace(/_/g, ' ')} {l.method ?? '—'} {deviceLabel(l.userAgent)} {l.ip ?? '—'}
No sign-in events yet.
); }