-
-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: session management in ui (#416)
* patch: issue deletion * feat: update client * chore: admin can delete any comment (#413) * chore: add on hold status (#415) * feat: follow an issue (#414) * patch: issue deletion * feat: update client * feat: follow an issue * feat: notifications when following * feat: see who is subscribed to this issue * patch: on hold * patch: migratiom * patch: fix notififaction * patch: remove dupe code * patch: fix null check * patch: remove code * feat: session management
- Loading branch information
Showing
6 changed files
with
195 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
apps/api/src/prisma/migrations/20241116014522_hold/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterEnum | ||
ALTER TYPE "TicketStatus" ADD VALUE 'hold'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -417,6 +417,7 @@ enum Hook { | |
} | ||
|
||
enum TicketStatus { | ||
hold | ||
needs_support | ||
in_progress | ||
in_review | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { toast } from "@/shadcn/hooks/use-toast"; | ||
import { Button } from "@/shadcn/ui/button"; | ||
import { getCookie } from "cookies-next"; | ||
import { useEffect, useState } from "react"; | ||
|
||
interface Session { | ||
id: string; | ||
userAgent: string; | ||
ipAddress: string; | ||
createdAt: string; | ||
expires: string; | ||
} | ||
|
||
function getPrettyUserAgent(userAgent: string) { | ||
// Extract browser and OS | ||
const browser = | ||
userAgent | ||
.match(/(Chrome|Safari|Firefox|Edge)\/[\d.]+/)?.[0] | ||
.split("/")[0] ?? "Unknown Browser"; | ||
const os = userAgent.match(/\((.*?)\)/)?.[1].split(";")[0] ?? "Unknown OS"; | ||
|
||
return `${browser} on ${os}`; | ||
} | ||
|
||
export default function Sessions() { | ||
const [sessions, setSessions] = useState<Session[]>([]); | ||
|
||
const fetchSessions = async () => { | ||
try { | ||
const response = await fetch("/api/v1/auth/sessions", { | ||
headers: { | ||
Authorization: `Bearer ${getCookie("session")}`, | ||
}, | ||
}); | ||
if (!response.ok) { | ||
throw new Error("Failed to fetch sessions"); | ||
} | ||
const data = await response.json(); | ||
setSessions(data.sessions); | ||
} catch (error) { | ||
console.error("Error fetching sessions:", error); | ||
|
||
toast({ | ||
variant: "destructive", | ||
title: "Error fetching sessions", | ||
description: "Please try again later", | ||
}); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
fetchSessions(); | ||
}, []); | ||
|
||
const revokeSession = async (sessionId: string) => { | ||
try { | ||
const response = await fetch(`/api/v1/auth/sessions/${sessionId}`, { | ||
headers: { | ||
Authorization: `Bearer ${getCookie("session")}`, | ||
}, | ||
method: "DELETE", | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error("Failed to revoke session"); | ||
} | ||
|
||
toast({ | ||
title: "Session revoked", | ||
description: "The session has been revoked", | ||
}); | ||
|
||
fetchSessions(); | ||
} catch (error) { | ||
console.error("Error revoking session:", error); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="p-6"> | ||
<div className="flex flex-col space-y-1 mb-4"> | ||
<h1 className="text-2xl font-bold">Active Sessions</h1> | ||
<span className="text-sm text-foreground"> | ||
Devices you are logged in to | ||
</span> | ||
</div> | ||
<div className="space-y-4"> | ||
{sessions && | ||
sessions.map((session) => ( | ||
<div | ||
key={session.id} | ||
className="flex flex-row items-center justify-between p-4 border rounded-lg group" | ||
> | ||
<div> | ||
<div className="text-base font-bold"> | ||
{session.ipAddress === "::1" | ||
? "Localhost" | ||
: session.ipAddress} | ||
</div> | ||
<div className="font-bold text-xs"> | ||
{getPrettyUserAgent(session.userAgent)} | ||
</div> | ||
<div className="text-xs text-foreground"> | ||
Created: {new Date(session.createdAt).toLocaleString("en-GB")} | ||
</div> | ||
<div className="text-xs text-foreground"> | ||
Expires: {new Date(session.expires).toLocaleString("en-GB")} | ||
</div> | ||
</div> | ||
<div className="opacity-0 group-hover:opacity-100 transition-opacity"> | ||
<Button | ||
size="sm" | ||
onClick={() => revokeSession(session.id)} | ||
variant="destructive" | ||
> | ||
Revoke | ||
</Button> | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
} |