"use client" import type { Passkey } from "@better-auth/passkey" import { FingerprintIcon, Loader2 } from "lucide-react" import { useContext, useState } from "react" import { AuthUIContext } from "../../../lib/auth-ui-provider" import { cn, getLocalizedError } from "../../../lib/utils" import type { AuthLocalization } from "../../../localization/auth-localization" import { Button } from "../../ui/button" import { Card } from "../../ui/card" import { SessionFreshnessDialog } from "../shared/session-freshness-dialog" import type { SettingsCardClassNames } from "../shared/settings-card" export interface PasskeyCellProps { className?: string classNames?: SettingsCardClassNames localization?: Partial passkey: Passkey } export function PasskeyCell({ className, classNames, localization, passkey }: PasskeyCellProps) { const { freshAge, hooks: { useSession, useListPasskeys }, localization: contextLocalization, mutators: { deletePasskey }, toast, localizeErrors } = useContext(AuthUIContext) localization = { ...contextLocalization, ...localization } const { refetch } = useListPasskeys() const { data: sessionData } = useSession() const session = sessionData?.session const isFresh = session ? Date.now() - new Date(session?.createdAt).getTime() < freshAge * 1000 : false const [showFreshnessDialog, setShowFreshnessDialog] = useState(false) const [isLoading, setIsLoading] = useState(false) const handleDeletePasskey = async () => { // If session isn't fresh, show the freshness dialog if (!isFresh) { setShowFreshnessDialog(true) return } setIsLoading(true) try { await deletePasskey({ id: passkey.id }) refetch?.() } catch (error) { setIsLoading(false) toast({ variant: "error", message: getLocalizedError({ error, localization, localizeErrors }) }) } } return ( <>
{new Date(passkey.createdAt).toLocaleString()}
) }