mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-12 07:53:00 +00:00
108 lines
3.3 KiB
TypeScript
108 lines
3.3 KiB
TypeScript
import {
|
|
getBrandingSettings,
|
|
getLoginSettings,
|
|
getSession,
|
|
getUserByID,
|
|
listAuthenticationMethodTypes,
|
|
server,
|
|
} from "#/lib/zitadel";
|
|
import Alert from "#/ui/Alert";
|
|
import ChooseSecondFactorToSetup from "#/ui/ChooseSecondFactorToSetup";
|
|
import DynamicTheme from "#/ui/DynamicTheme";
|
|
import UserAvatar from "#/ui/UserAvatar";
|
|
import {
|
|
getMostRecentCookieWithLoginname,
|
|
getSessionCookieById,
|
|
} from "#/utils/cookies";
|
|
|
|
export default async function Page({
|
|
searchParams,
|
|
}: {
|
|
searchParams: Record<string | number | symbol, string | undefined>;
|
|
}) {
|
|
const { loginName, checkAfter, authRequestId, organization, sessionId } =
|
|
searchParams;
|
|
|
|
const sessionFactors = sessionId
|
|
? await loadSessionById(sessionId, organization)
|
|
: await loadSessionByLoginname(loginName, organization);
|
|
|
|
async function loadSessionByLoginname(
|
|
loginName?: string,
|
|
organization?: string
|
|
) {
|
|
const recent = await getMostRecentCookieWithLoginname(
|
|
loginName,
|
|
organization
|
|
);
|
|
return getSession(server, recent.id, recent.token).then((response) => {
|
|
if (response?.session && response.session.factors?.user?.id) {
|
|
return listAuthenticationMethodTypes(
|
|
response.session.factors.user.id
|
|
).then((methods) => {
|
|
return {
|
|
factors: response.session?.factors,
|
|
authMethods: methods.authMethodTypes ?? [],
|
|
};
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
async function loadSessionById(sessionId: string, organization?: string) {
|
|
const recent = await getSessionCookieById(sessionId, organization);
|
|
return getSession(server, recent.id, recent.token).then((response) => {
|
|
if (response?.session && response.session.factors?.user?.id) {
|
|
return listAuthenticationMethodTypes(
|
|
response.session.factors.user.id
|
|
).then((methods) => {
|
|
return {
|
|
factors: response.session?.factors,
|
|
authMethods: methods.authMethodTypes ?? [],
|
|
};
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
const branding = await getBrandingSettings(server, organization);
|
|
const loginSettings = await getLoginSettings(server, organization);
|
|
|
|
return (
|
|
<DynamicTheme branding={branding}>
|
|
<div className="flex flex-col items-center space-y-4">
|
|
<h1>Set up 2-Factor</h1>
|
|
|
|
<p className="ztdl-p">Choose one of the following second factors.</p>
|
|
|
|
{sessionFactors && (
|
|
<UserAvatar
|
|
loginName={loginName ?? sessionFactors.factors?.user?.loginName}
|
|
displayName={sessionFactors.factors?.user?.displayName}
|
|
showDropdown
|
|
searchParams={searchParams}
|
|
></UserAvatar>
|
|
)}
|
|
|
|
{!(loginName || sessionId) && (
|
|
<Alert>Provide your active session as loginName param</Alert>
|
|
)}
|
|
|
|
{loginSettings && sessionFactors ? (
|
|
<ChooseSecondFactorToSetup
|
|
loginName={loginName}
|
|
sessionId={sessionId}
|
|
authRequestId={authRequestId}
|
|
organization={organization}
|
|
loginSettings={loginSettings}
|
|
userMethods={sessionFactors.authMethods ?? []}
|
|
checkAfter={checkAfter === "true"}
|
|
></ChooseSecondFactorToSetup>
|
|
) : (
|
|
<Alert>No second factors available to setup.</Alert>
|
|
)}
|
|
</div>
|
|
</DynamicTheme>
|
|
);
|
|
}
|