Files
zitadel/apps/login/src/app/(login)/idp/[provider]/success/page.tsx

202 lines
6.6 KiB
TypeScript
Raw Normal View History

2024-09-11 17:12:43 +02:00
import { idpTypeToIdentityProviderType, PROVIDER_MAPPING } from "@/lib/idp";
import {
addIDPLink,
createUser,
getBrandingSettings,
2024-08-21 11:11:00 +02:00
getIDPByID,
2024-08-21 11:18:12 +02:00
listUsers,
retrieveIDPIntent,
} from "@/lib/zitadel";
2024-05-13 16:17:12 -04:00
import Alert, { AlertType } from "@/ui/Alert";
import DynamicTheme from "@/ui/DynamicTheme";
import IdpSignin from "@/ui/IdpSignin";
2024-08-21 12:13:24 +02:00
import { AutoLinkingOption } from "@zitadel/proto/zitadel/idp/v2/idp_pb";
2023-07-28 15:23:17 +02:00
export default async function Page({
searchParams,
params,
}: {
searchParams: Record<string | number | symbol, string | undefined>;
2024-08-21 14:15:44 +02:00
params: { provider: string };
2023-07-28 15:23:17 +02:00
}) {
const { id, token, authRequestId, organization } = searchParams;
2023-07-28 15:23:17 +02:00
const { provider } = params;
const branding = await getBrandingSettings(organization);
2023-07-28 15:23:17 +02:00
if (provider && id && token) {
2024-03-19 14:15:54 +01:00
return retrieveIDPIntent(id, token)
2024-08-21 11:11:00 +02:00
.then(async (resp) => {
2024-03-19 14:15:54 +01:00
const { idpInformation, userId } = resp;
2024-05-03 10:09:18 +02:00
2024-08-21 11:11:00 +02:00
if (userId) {
// TODO: update user if idp.options.isAutoUpdate is true
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>
<IdpSignin
userId={userId}
idpIntent={{ idpIntentId: id, idpIntentToken: token }}
authRequestId={authRequestId}
/>
</div>
</DynamicTheme>
);
}
2024-03-19 14:15:54 +01:00
if (idpInformation) {
2024-08-21 11:11:00 +02:00
const idp = await getIDPByID(idpInformation.idpId);
const options = idp?.config?.options;
2024-09-11 17:12:43 +02:00
if (!idp) {
throw new Error("IDP not found");
}
const providerType = idpTypeToIdentityProviderType(idp.type);
2024-08-21 11:11:00 +02:00
// search for potential user via username, then link
if (options?.isLinkingAllowed) {
2024-08-21 12:13:24 +02:00
let foundUser;
const email =
2024-09-11 17:12:43 +02:00
PROVIDER_MAPPING[providerType](idpInformation).email?.email;
2024-08-21 12:13:24 +02:00
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) => {
2024-08-21 11:18:12 +02:00
return response.result ? response.result[0] : null;
2024-08-21 12:13:24 +02:00
});
} else {
foundUser = await listUsers({
userName: idpInformation.userName,
email,
}).then((response) => {
return response.result ? response.result[0] : null;
});
}
2024-08-21 11:18:12 +02:00
if (foundUser) {
const idpLink = await addIDPLink(
{
id: idpInformation.idpId,
userId: idpInformation.userId,
userName: idpInformation.userName,
},
2024-08-21 11:36:28 +02:00
foundUser.userId,
2024-08-21 11:18:12 +02:00
).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 (idpLink) {
return (
2024-08-21 12:13:24 +02:00
// TODO: possibily login user now
2024-08-21 11:18:12 +02:00
<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>
2024-08-21 11:11:00 +02:00
</div>
2024-08-21 11:18:12 +02:00
</DynamicTheme>
);
}
2024-08-21 11:11:00 +02:00
}
2024-08-21 16:08:37 +02:00
}
if (options?.isCreationAllowed && options.isAutoCreation) {
2024-09-11 17:12:43 +02:00
const newUser = await createUser(providerType, idpInformation);
2024-08-21 11:11:00 +02:00
2024-08-21 16:08:37 +02:00
if (newUser) {
2024-08-21 11:11:00 +02:00
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>
);
}
2024-03-15 17:21:21 +01:00
}
2024-08-21 11:11:00 +02:00
// 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>
);
2023-07-31 11:56:23 +02:00
} else {
2024-08-21 11:11:00 +02:00
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>
);
2023-07-31 11:56:23 +02:00
}
})
2024-03-19 14:15:54 +01:00
.catch((error) => {
2023-07-31 11:56:23 +02:00
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>An error occurred</h1>
<div className="w-full">
{
<Alert type={AlertType.ALERT}>
{JSON.stringify(error.message)}
</Alert>
}
</div>
2023-07-31 11:56:23 +02:00
</div>
</DynamicTheme>
2023-07-31 11:56:23 +02:00
);
});
2023-07-28 15:23:17 +02:00
} else {
return (
2024-05-03 10:09:18 +02:00
<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>
2023-07-28 15:23:17 +02:00
);
}
}