| 1 | "use client"; |
| 2 | |
| 3 | import { useAuth, useRevokeSession, useSession } from "@better-auth-ui/react"; |
| 4 | import type { Session |
| 5 | import Bowser from "bowser |
| 6 | import { LogOut, Monitor |
| 7 | import { toast } |
| 8 | |
| 9 | import { Badge } |
| 10 | import { Button } |
| 11 | import { |
| 12 | Item, |
| 13 | ItemActions, |
| 14 | ItemContent, |
| 15 | ItemDescription, |
| 16 | ItemMedia, |
| 17 | ItemTitle, |
| 18 | } from "@/components/ui/item"; |
| 19 | import { Spinner } |
| 20 | |
| 21 | function timeAgo(date: |
| 22 | const seconds = |
| 23 | const rtf = |
| 24 | |
| 25 | const UNITS: |
| 26 | ["year", 31536000], |
| 27 | ["month", 2592000], |
| 28 | ["week", 604800], |
| 29 | ["day", 86400], |
| 30 | ["hour", 3600], |
| 31 | ["minute", 60], |
| 32 | ["second", 1], |
| 33 | ]; |
| 34 | |
| 35 | for (const [unit |
| 36 | if (seconds >= |
| 37 | return rtf.format |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return rtf.format |
| 42 | } |
| 43 | |
| 44 | export type ActiveSessionProps |
| 45 | activeSession: Session |
| 46 | }; |
| 47 | |
| 48 | /** Render a single active session row with device info and revoke control. */ |
| 49 | export function ActiveSession({ |
| 50 | const { authClient, |
| 51 | useAuth(); |
| 52 | const { data: |
| 53 | |
| 54 | const { mutate: |
| 55 | authClient, |
| 56 | { |
| 57 | onSuccess: () => |
| 58 | toast.success(localization |
| 59 | }, |
| 60 | ); |
| 61 | |
| 62 | const isCurrentSession = |
| 63 | const ua = |
| 64 | const isMobile = |
| 65 | ua.platform.type |
| 66 | |
| 67 | return ( |
| 68 | <Item> |
| 69 | <ItemMedia variant=" |
| 70 | {isMobile ? <Smartphone |
| 71 | </ItemMedia> |
| 72 | <ItemContent> |
| 73 | <ItemTitle> |
| 74 | {ua.browser.name |
| 75 | {ua.os.name |
| 76 | </ItemTitle> |
| 77 | {isCurrentSession ? ( |
| 78 | <Badge variant=" |
| 79 | {localization.settings.currentSession |
| 80 | </Badge> |
| 81 | ) : ( |
| 82 | activeSession.createdAt && |
| 83 | <ItemDescription className=" |
| 84 | {timeAgo(activeSession.createdAt |
| 85 | </ItemDescription> |
| 86 | ) |
| 87 | )} |
| 88 | </ItemContent> |
| 89 | <ItemActions> |
| 90 | <Button |
| 91 | variant="outline" |
| 92 | size="sm" |
| 93 | onClick={() => |
| 94 | isCurrentSession |
| 95 | ? navigate({ |
| 96 | to: `${basePaths. |
| 97 | }) |
| 98 | : revokeSession(activeSession) |
| 99 | } |
| 100 | disabled={isRevoking} |
| 101 | aria-label={ |
| 102 | isCurrentSession |
| 103 | ? localization.auth. |
| 104 | : localization.settings. |
| 105 | } |
| 106 | > |
| 107 | {isRevoking ? <Spinner |
| 108 | |
| 109 | {isCurrentSession |
| 110 | ? localization.auth. |
| 111 | : localization.settings. |
| 112 | </Button> |
| 113 | </ItemActions> |
| 114 | </Item> |
| 115 | ); |
| 116 | } |