2023-06-06 17:11:49 +02:00
|
|
|
import { Session } from "@zitadel/server";
|
2023-05-17 15:25:25 +02:00
|
|
|
import { listSessions, server } from "#/lib/zitadel";
|
2023-05-22 11:48:18 +02:00
|
|
|
import Alert from "#/ui/Alert";
|
2023-05-17 15:25:25 +02:00
|
|
|
import { getAllSessionIds } from "#/utils/cookies";
|
2023-06-06 17:11:49 +02:00
|
|
|
import { UserPlusIcon } from "@heroicons/react/24/outline";
|
2023-05-17 15:25:25 +02:00
|
|
|
import Link from "next/link";
|
2023-06-06 17:11:49 +02:00
|
|
|
import SessionItem from "#/ui/SessionItem";
|
2023-05-17 15:25:25 +02:00
|
|
|
|
2023-05-24 16:27:35 +02:00
|
|
|
async function loadSessions(): Promise<Session[]> {
|
2023-05-23 09:22:15 +02:00
|
|
|
const ids = await getAllSessionIds();
|
2023-05-17 15:25:25 +02:00
|
|
|
|
|
|
|
|
if (ids && ids.length) {
|
2023-05-24 16:27:35 +02:00
|
|
|
const response = await listSessions(
|
2023-05-17 15:25:25 +02:00
|
|
|
server,
|
|
|
|
|
ids.filter((id: string | undefined) => !!id)
|
2023-05-23 09:22:15 +02:00
|
|
|
);
|
2023-05-24 16:27:35 +02:00
|
|
|
return response?.sessions ?? [];
|
2023-05-17 15:25:25 +02:00
|
|
|
} else {
|
2023-05-25 08:40:53 +02:00
|
|
|
console.info("No session cookie found.");
|
2023-05-17 15:25:25 +02:00
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default async function Page() {
|
2023-06-06 17:11:49 +02:00
|
|
|
let sessions = await loadSessions();
|
2023-05-17 15:25:25 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex flex-col items-center space-y-4">
|
|
|
|
|
<h1>Accounts</h1>
|
|
|
|
|
<p className="ztdl-p mb-6 block">Use your ZITADEL Account</p>
|
|
|
|
|
|
2023-05-23 09:31:56 +02:00
|
|
|
<div className="flex flex-col w-full space-y-2">
|
2023-05-17 15:25:25 +02:00
|
|
|
{sessions ? (
|
2023-05-24 23:00:55 +02:00
|
|
|
sessions
|
|
|
|
|
.filter((session) => session?.factors?.user?.loginName)
|
|
|
|
|
.map((session, index) => {
|
|
|
|
|
return (
|
2023-06-06 17:11:49 +02:00
|
|
|
<SessionItem
|
|
|
|
|
session={session}
|
|
|
|
|
reload={async () => {
|
|
|
|
|
"use server";
|
|
|
|
|
sessions = sessions.filter((s) => s.id !== session.id);
|
|
|
|
|
}}
|
2023-05-24 23:00:55 +02:00
|
|
|
key={"session-" + index}
|
2023-06-06 17:11:49 +02:00
|
|
|
/>
|
2023-05-24 23:00:55 +02:00
|
|
|
);
|
|
|
|
|
})
|
2023-05-17 15:25:25 +02:00
|
|
|
) : (
|
2023-05-22 11:48:18 +02:00
|
|
|
<Alert>No Sessions available!</Alert>
|
2023-05-17 15:25:25 +02:00
|
|
|
)}
|
2023-05-23 09:22:15 +02:00
|
|
|
<Link href="/username">
|
2023-05-23 09:31:56 +02:00
|
|
|
<div className="flex flex-row items-center py-3 px-4 hover:bg-black/10 dark:hover:bg-white/10 rounded-md transition-all">
|
2023-05-23 09:22:15 +02:00
|
|
|
<div className="w-8 h-8 mr-4 flex flex-row justify-center items-center rounded-full bg-black/5 dark:bg-white/5">
|
|
|
|
|
<UserPlusIcon className="h-5 w-5" />
|
|
|
|
|
</div>
|
|
|
|
|
<span className="text-sm">Add another account</span>
|
|
|
|
|
</div>
|
|
|
|
|
</Link>
|
2023-05-17 15:25:25 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|