Files
zitadel/apps/login/src/components/session-item.tsx

148 lines
4.4 KiB
TypeScript
Raw Normal View History

2023-06-06 17:11:49 +02:00
"use client";
import { sendLoginname } from "@/lib/server/loginname";
import { cleanupSession, continueWithSession } from "@/lib/server/session";
2024-09-05 13:48:33 +02:00
import { XCircleIcon } from "@heroicons/react/24/outline";
2024-09-19 17:16:56 +02:00
import { Timestamp, timestampDate } from "@zitadel/client";
2024-09-05 13:48:33 +02:00
import { Session } from "@zitadel/proto/zitadel/session/v2/session_pb";
import moment from "moment";
2024-11-26 13:49:20 +01:00
import { useRouter } from "next/navigation";
2023-06-06 17:11:49 +02:00
import { useState } from "react";
2024-09-26 22:50:55 -04:00
import { Avatar } from "./avatar";
2023-06-06 17:11:49 +02:00
2024-09-19 17:16:56 +02:00
export function isSessionValid(session: Partial<Session>): {
valid: boolean;
verifiedAt?: Timestamp;
} {
2024-09-19 16:29:02 +02:00
const validPassword = session?.factors?.password?.verifiedAt;
const validPasskey = session?.factors?.webAuthN?.verifiedAt;
2024-12-03 10:07:14 +01:00
const validIDP = session?.factors?.intent?.verifiedAt;
2024-09-19 16:29:02 +02:00
const stillValid = session.expirationDate
? timestampDate(session.expirationDate) > new Date()
: true;
2024-12-03 10:07:14 +01:00
const verifiedAt = validPassword || validPasskey || validIDP;
const valid = !!((validPassword || validPasskey || validIDP) && stillValid);
2024-09-19 16:29:02 +02:00
return { valid, verifiedAt };
}
2024-09-26 22:50:55 -04:00
export function SessionItem({
2023-06-06 17:11:49 +02:00
session,
reload,
authRequestId,
2023-06-06 17:11:49 +02:00
}: {
session: Session;
reload: () => void;
authRequestId?: string;
2023-06-06 17:11:49 +02:00
}) {
const [loading, setLoading] = useState<boolean>(false);
async function clearSession(id: string) {
setLoading(true);
2024-09-03 10:24:05 +02:00
const response = await cleanupSession({
sessionId: id,
2024-11-11 10:45:32 +01:00
})
.catch((error) => {
setError(error.message);
2024-11-26 11:15:57 +01:00
return;
2024-11-11 10:45:32 +01:00
})
.finally(() => {
setLoading(false);
});
2023-06-06 17:11:49 +02:00
2024-09-03 10:24:05 +02:00
return response;
2023-06-06 17:11:49 +02:00
}
const { valid, verifiedAt } = isSessionValid(session);
2023-06-06 17:11:49 +02:00
2024-09-03 10:24:05 +02:00
const [error, setError] = useState<string | null>(null);
2024-11-26 13:49:20 +01:00
const router = useRouter();
2023-06-06 17:11:49 +02:00
return (
<button
onClick={async () => {
if (valid && session?.factors?.user) {
return continueWithSession({
...session,
authRequestId: authRequestId,
});
} else if (session.factors?.user) {
setLoading(true);
const res = await sendLoginname({
loginName: session.factors?.user?.loginName,
organization: session.factors.user.organizationId,
authRequestId: authRequestId,
})
.catch(() => {
setError("An internal error occurred");
2024-11-26 11:15:57 +01:00
return;
})
.finally(() => {
setLoading(false);
});
if (res?.redirect) {
2024-11-26 13:49:20 +01:00
return router.push(res.redirect);
}
if (res?.error) {
setError(res.error);
2024-11-26 11:15:57 +01:00
return;
}
}
}}
2023-06-06 17:11:49 +02:00
className="group flex flex-row items-center bg-background-light-400 dark:bg-background-dark-400 border border-divider-light hover:shadow-lg dark:hover:bg-white/10 py-2 px-4 rounded-md transition-all"
>
<div className="pr-4">
<Avatar
size="small"
loginName={session.factors?.user?.loginName as string}
name={session.factors?.user?.displayName ?? ""}
/>
</div>
<div className="flex flex-col items-start overflow-hidden">
2023-06-06 17:11:49 +02:00
<span className="">{session.factors?.user?.displayName}</span>
2024-09-18 14:13:04 +02:00
<span className="text-xs opacity-80 text-ellipsis">
2023-06-06 17:11:49 +02:00
{session.factors?.user?.loginName}
</span>
2024-12-03 10:07:14 +01:00
{valid ? (
2024-09-18 14:13:04 +02:00
<span className="text-xs opacity-80 text-ellipsis">
{verifiedAt && moment(timestampDate(verifiedAt)).fromNow()}
2023-06-06 17:11:49 +02:00
</span>
2024-12-03 10:07:14 +01:00
) : (
2024-12-09 10:48:50 +01:00
verifiedAt && (
<span className="text-xs opacity-80 text-ellipsis">
expired{" "}
{session.expirationDate &&
moment(timestampDate(session.expirationDate)).fromNow()}
</span>
)
2023-06-06 17:11:49 +02:00
)}
</div>
<span className="flex-grow"></span>
<div className="relative flex flex-row items-center">
{valid ? (
2023-06-06 17:11:49 +02:00
<div className="absolute h-2 w-2 bg-green-500 rounded-full mx-2 transform right-0 group-hover:right-6 transition-all"></div>
) : (
<div className="absolute h-2 w-2 bg-red-500 rounded-full mx-2 transform right-0 group-hover:right-6 transition-all"></div>
)}
<XCircleIcon
className="hidden group-hover:block h-5 w-5 transition-all opacity-50 hover:opacity-100"
2024-08-14 09:48:40 +02:00
onClick={(event) => {
event.preventDefault();
2023-06-06 17:11:49 +02:00
clearSession(session.id).then(() => {
reload();
2024-08-14 09:48:40 +02:00
});
}}
2023-06-06 17:11:49 +02:00
/>
</div>
</button>
2023-06-06 17:11:49 +02:00
);
}