|
| 1 | +import { toast } from "@/shadcn/hooks/use-toast"; |
| 2 | +import { Button } from "@/shadcn/ui/button"; |
| 3 | +import { getCookie } from "cookies-next"; |
| 4 | +import { useEffect, useState } from "react"; |
| 5 | + |
| 6 | +interface Session { |
| 7 | + id: string; |
| 8 | + userAgent: string; |
| 9 | + ipAddress: string; |
| 10 | + createdAt: string; |
| 11 | + expires: string; |
| 12 | +} |
| 13 | + |
| 14 | +function getPrettyUserAgent(userAgent: string) { |
| 15 | + // Extract browser and OS |
| 16 | + const browser = |
| 17 | + userAgent |
| 18 | + .match(/(Chrome|Safari|Firefox|Edge)\/[\d.]+/)?.[0] |
| 19 | + .split("/")[0] ?? "Unknown Browser"; |
| 20 | + const os = userAgent.match(/\((.*?)\)/)?.[1].split(";")[0] ?? "Unknown OS"; |
| 21 | + |
| 22 | + return `${browser} on ${os}`; |
| 23 | +} |
| 24 | + |
| 25 | +export default function Sessions() { |
| 26 | + const [sessions, setSessions] = useState<Session[]>([]); |
| 27 | + |
| 28 | + const fetchSessions = async () => { |
| 29 | + try { |
| 30 | + const response = await fetch("/api/v1/auth/sessions", { |
| 31 | + headers: { |
| 32 | + Authorization: `Bearer ${getCookie("session")}`, |
| 33 | + }, |
| 34 | + }); |
| 35 | + if (!response.ok) { |
| 36 | + throw new Error("Failed to fetch sessions"); |
| 37 | + } |
| 38 | + const data = await response.json(); |
| 39 | + setSessions(data.sessions); |
| 40 | + } catch (error) { |
| 41 | + console.error("Error fetching sessions:", error); |
| 42 | + |
| 43 | + toast({ |
| 44 | + variant: "destructive", |
| 45 | + title: "Error fetching sessions", |
| 46 | + description: "Please try again later", |
| 47 | + }); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + fetchSessions(); |
| 53 | + }, []); |
| 54 | + |
| 55 | + const revokeSession = async (sessionId: string) => { |
| 56 | + try { |
| 57 | + const response = await fetch(`/api/v1/auth/sessions/${sessionId}`, { |
| 58 | + headers: { |
| 59 | + Authorization: `Bearer ${getCookie("session")}`, |
| 60 | + }, |
| 61 | + method: "DELETE", |
| 62 | + }); |
| 63 | + |
| 64 | + if (!response.ok) { |
| 65 | + throw new Error("Failed to revoke session"); |
| 66 | + } |
| 67 | + |
| 68 | + toast({ |
| 69 | + title: "Session revoked", |
| 70 | + description: "The session has been revoked", |
| 71 | + }); |
| 72 | + |
| 73 | + fetchSessions(); |
| 74 | + } catch (error) { |
| 75 | + console.error("Error revoking session:", error); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + return ( |
| 80 | + <div className="p-6"> |
| 81 | + <div className="flex flex-col space-y-1 mb-4"> |
| 82 | + <h1 className="text-2xl font-bold">Active Sessions</h1> |
| 83 | + <span className="text-sm text-foreground"> |
| 84 | + Devices you are logged in to |
| 85 | + </span> |
| 86 | + </div> |
| 87 | + <div className="space-y-4"> |
| 88 | + {sessions && |
| 89 | + sessions.map((session) => ( |
| 90 | + <div |
| 91 | + key={session.id} |
| 92 | + className="flex flex-row items-center justify-between p-4 border rounded-lg group" |
| 93 | + > |
| 94 | + <div> |
| 95 | + <div className="text-base font-bold"> |
| 96 | + {session.ipAddress === "::1" |
| 97 | + ? "Localhost" |
| 98 | + : session.ipAddress} |
| 99 | + </div> |
| 100 | + <div className="font-bold text-xs"> |
| 101 | + {getPrettyUserAgent(session.userAgent)} |
| 102 | + </div> |
| 103 | + <div className="text-xs text-foreground"> |
| 104 | + Created: {new Date(session.createdAt).toLocaleString("en-GB")} |
| 105 | + </div> |
| 106 | + <div className="text-xs text-foreground"> |
| 107 | + Expires: {new Date(session.expires).toLocaleString("en-GB")} |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + <div className="opacity-0 group-hover:opacity-100 transition-opacity"> |
| 111 | + <Button |
| 112 | + size="sm" |
| 113 | + onClick={() => revokeSession(session.id)} |
| 114 | + variant="destructive" |
| 115 | + > |
| 116 | + Revoke |
| 117 | + </Button> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + ))} |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + ); |
| 124 | +} |
0 commit comments