try to fix unit tests, idp, password, accounts translations

This commit is contained in:
peintnermax
2024-10-09 14:03:01 +02:00
parent 93b2ebf75e
commit 12df4eec88
11 changed files with 214 additions and 174 deletions

View File

@@ -11,7 +11,24 @@ import {
retrieveIDPIntent,
} from "@/lib/zitadel";
import { AutoLinkingOption } from "@zitadel/proto/zitadel/idp/v2/idp_pb";
import { BrandingSettings } from "@zitadel/proto/zitadel/settings/v2/branding_settings_pb";
import { getLocale, getTranslations } from "next-intl/server";
async function loginFailed(branding?: BrandingSettings) {
const locale = getLocale();
const t = await getTranslations({ locale, namespace: "idp" });
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>{t("loginError.title")}</h1>
<div className="w-full">
{<Alert type={AlertType.ALERT}>{t("loginError.title")}</Alert>}
</div>
</div>
</DynamicTheme>
);
}
export default async function Page({
searchParams,
params,
@@ -19,166 +36,96 @@ export default async function Page({
searchParams: Record<string | number | symbol, string | undefined>;
params: { provider: string };
}) {
const locale = getLocale();
const t = await getTranslations({ locale, namespace: "idp" });
const { id, token, authRequestId, organization } = searchParams;
const { provider } = params;
const branding = await getBrandingSettings(organization);
if (provider && id && token) {
return retrieveIDPIntent(id, token)
.then(async (resp) => {
const { idpInformation, userId } = resp;
if (userId) {
// TODO: update user if idp.options.isAutoUpdate is true
if (!provider || !id || !token) {
return loginFailed(branding);
}
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>Login successful</h1>
<div>You have successfully been loggedIn!</div>
const intent = await retrieveIDPIntent(id, token);
<IdpSignin
userId={userId}
idpIntent={{ idpIntentId: id, idpIntentToken: token }}
authRequestId={authRequestId}
/>
</div>
</DynamicTheme>
);
}
const { idpInformation, userId } = intent;
if (idpInformation) {
const idp = await getIDPByID(idpInformation.idpId);
const options = idp?.config?.options;
if (userId && id && token) {
// TODO: update user if idp.options.isAutoUpdate is true
if (!idp) {
throw new Error("IDP not found");
}
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>{t("loginSuccess.title")}</h1>
<div>{t("loginSuccess.description")}</div>
const providerType = idpTypeToIdentityProviderType(idp.type);
<IdpSignin
userId={userId}
idpIntent={{ idpIntentId: id, idpIntentToken: token }}
authRequestId={authRequestId}
/>
</div>
</DynamicTheme>
);
}
// search for potential user via username, then link
if (options?.isLinkingAllowed) {
let foundUser;
const email =
PROVIDER_MAPPING[providerType](idpInformation).email?.email;
if (!idpInformation) {
return loginFailed(branding);
}
if (options.autoLinking === AutoLinkingOption.EMAIL && email) {
foundUser = await listUsers({ email }).then((response) => {
return response.result ? response.result[0] : null;
});
} else if (options.autoLinking === AutoLinkingOption.USERNAME) {
foundUser = await listUsers(
options.autoLinking === AutoLinkingOption.USERNAME
? { userName: idpInformation.userName }
: { email },
).then((response) => {
return response.result ? response.result[0] : null;
});
} else {
foundUser = await listUsers({
userName: idpInformation.userName,
email,
}).then((response) => {
return response.result ? response.result[0] : null;
});
}
const idp = await getIDPByID(idpInformation.idpId);
const options = idp?.config?.options;
if (foundUser) {
const idpLink = await addIDPLink(
{
id: idpInformation.idpId,
userId: idpInformation.userId,
userName: idpInformation.userName,
},
foundUser.userId,
).catch((error) => {
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>Linking failed</h1>
<div className="w-full">
{
<Alert type={AlertType.ALERT}>
{JSON.stringify(error.message)}
</Alert>
}
</div>
</div>
</DynamicTheme>
);
});
if (!idp) {
throw new Error("IDP not found");
}
if (idpLink) {
return (
// TODO: possibily login user now
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>Account successfully linked</h1>
<div>Your account has successfully been linked!</div>
</div>
</DynamicTheme>
);
}
}
}
const providerType = idpTypeToIdentityProviderType(idp.type);
if (options?.isCreationAllowed && options.isAutoCreation) {
const newUser = await createUser(providerType, idpInformation);
// search for potential user via username, then link
if (options?.isLinkingAllowed) {
let foundUser;
const email = PROVIDER_MAPPING[providerType](idpInformation).email?.email;
if (newUser) {
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>Register successful</h1>
<div>You have successfully been registered!</div>
</div>
</DynamicTheme>
);
}
}
if (options.autoLinking === AutoLinkingOption.EMAIL && email) {
foundUser = await listUsers({ email }).then((response) => {
return response.result ? response.result[0] : null;
});
} else if (options.autoLinking === AutoLinkingOption.USERNAME) {
foundUser = await listUsers(
options.autoLinking === AutoLinkingOption.USERNAME
? { userName: idpInformation.userName }
: { email },
).then((response) => {
return response.result ? response.result[0] : null;
});
} else {
foundUser = await listUsers({
userName: idpInformation.userName,
email,
}).then((response) => {
return response.result ? response.result[0] : null;
});
}
// return login failed if no linking or creation is allowed and no user was found
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>Login failed</h1>
<div className="w-full">
{
<Alert type={AlertType.ALERT}>
User could not be logged in
</Alert>
}
</div>
</div>
</DynamicTheme>
);
} else {
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>Login failed</h1>
<div className="w-full">
{
<Alert type={AlertType.ALERT}>
Could not get user information
</Alert>
}
</div>
</div>
</DynamicTheme>
);
}
})
.catch((error) => {
if (foundUser) {
const idpLink = await addIDPLink(
{
id: idpInformation.idpId,
userId: idpInformation.userId,
userName: idpInformation.userName,
},
foundUser.userId,
).catch((error) => {
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>An error occurred</h1>
<h1>{t("linkingError.title")}</h1>
<div className="w-full">
{
<Alert type={AlertType.ALERT}>
{JSON.stringify(error.message)}
{t("linkingError.description")}
</Alert>
}
</div>
@@ -186,16 +133,36 @@ export default async function Page({
</DynamicTheme>
);
});
} else {
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<div className="flex flex-col items-center space-y-4">
<h1>Register</h1>
<p className="ztdl-p">No id and token received!</p>
</div>
</div>
</DynamicTheme>
);
if (idpLink) {
return (
// TODO: possibily login user now
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>{t("linkingSuccess.title")}</h1>
<div>{t("linkingSuccess.description")}</div>
</div>
</DynamicTheme>
);
}
}
}
if (options?.isCreationAllowed && options.isAutoCreation) {
const newUser = await createUser(providerType, idpInformation);
if (newUser) {
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>{t("registerSuccess.title")}</h1>
<div>{t("registerSuccess.description")}</div>
</div>
</DynamicTheme>
);
}
}
// return login failed if no linking or creation is allowed and no user was found
return loginFailed;
}