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

289 lines
8.8 KiB
TypeScript
Raw Normal View History

2024-05-13 16:17:12 -04:00
import { ProviderSlug } from "@/lib/demos";
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-06 09:34:45 +02:00
import { AddHumanUserRequest } from "@zitadel/proto/zitadel/user/v2/user_service_pb";
import { IDPInformation, IDPLink } from "@zitadel/proto/zitadel/user/v2/idp_pb";
2024-07-25 23:16:07 -04:00
import { PartialMessage } from "@zitadel/client";
2023-07-28 15:23:17 +02:00
const PROVIDER_MAPPING: {
[provider: string]: (
rI: IDPInformation,
) => PartialMessage<AddHumanUserRequest>;
2023-07-28 15:23:17 +02:00
} = {
[ProviderSlug.GOOGLE]: (idp: IDPInformation) => {
const rawInfo = idp.rawInformation?.toJson() as {
2024-08-09 10:09:10 +02:00
User: {
email: string;
name?: string;
given_name?: string;
family_name?: string;
};
};
const idpLink: PartialMessage<IDPLink> = {
idpId: idp.idpId,
userId: idp.userId,
userName: idp.userName,
};
const req: PartialMessage<AddHumanUserRequest> = {
username: idp.userName,
email: {
email: rawInfo.User?.email,
verification: { case: "isVerified", value: true },
},
// organisation: Organisation | undefined;
profile: {
displayName: rawInfo.User?.name ?? "",
givenName: rawInfo.User?.given_name ?? "",
familyName: rawInfo.User?.family_name ?? "",
},
idpLinks: [idpLink],
};
return req;
},
[ProviderSlug.AZURE]: (idp: IDPInformation) => {
const rawInfo = idp.rawInformation?.toJson() as {
2024-08-09 10:57:39 +02:00
mail: string;
displayName?: string;
givenName?: string;
surname?: string;
};
const idpLink: PartialMessage<IDPLink> = {
2023-07-28 15:23:17 +02:00
idpId: idp.idpId,
userId: idp.userId,
userName: idp.userName,
};
const req: PartialMessage<AddHumanUserRequest> = {
2023-07-28 15:23:17 +02:00
username: idp.userName,
email: {
2024-08-09 10:57:39 +02:00
email: rawInfo?.mail,
verification: { case: "isVerified", value: true },
2023-07-28 15:23:17 +02:00
},
// organisation: Organisation | undefined;
profile: {
2024-08-09 10:57:39 +02:00
displayName: rawInfo?.displayName ?? "",
givenName: rawInfo?.givenName ?? "",
familyName: rawInfo?.surname ?? "",
2023-07-28 15:23:17 +02:00
},
idpLinks: [idpLink],
};
2024-08-21 11:11:00 +02:00
2023-07-28 15:23:17 +02:00
return req;
},
[ProviderSlug.GITHUB]: (idp: IDPInformation) => {
const rawInfo = idp.rawInformation?.toJson() as {
email: string;
name: string;
};
const idpLink: PartialMessage<IDPLink> = {
2023-07-28 15:23:17 +02:00
idpId: idp.idpId,
userId: idp.userId,
userName: idp.userName,
};
const req: PartialMessage<AddHumanUserRequest> = {
2023-07-28 15:23:17 +02:00
username: idp.userName,
email: {
email: rawInfo?.email,
verification: { case: "isVerified", value: true },
2023-07-28 15:23:17 +02:00
},
// organisation: Organisation | undefined;
profile: {
displayName: rawInfo?.name ?? "",
givenName: rawInfo?.name ?? "",
familyName: rawInfo?.name ?? "",
2023-07-28 15:23:17 +02:00
},
idpLinks: [idpLink],
};
return req;
},
};
export default async function Page({
searchParams,
params,
}: {
searchParams: Record<string | number | symbol, string | undefined>;
params: { provider: ProviderSlug };
}) {
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;
// search for potential user via username, then link
if (options?.isLinkingAllowed) {
2024-08-21 11:18:12 +02:00
const foundUser = await listUsers(idpInformation.userName).then(
(response) => {
return response.result ? response.result[0] : null;
2024-08-21 11:11:00 +02:00
},
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 (
<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
}
} else if (options?.isCreationAllowed && options.isAutoCreation) {
const userId = await createUser(provider, idpInformation).catch(
(error) => {
2024-03-15 17:21:21 +01:00
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
2024-08-21 11:11:00 +02:00
<h1>Register failed</h1>
<div className="w-full">
{
<Alert type={AlertType.ALERT}>
{JSON.stringify(error.message)}
</Alert>
}
</div>
</div>
</DynamicTheme>
2024-03-15 17:21:21 +01:00
);
2024-08-21 11:11:00 +02:00
},
);
if (userId) {
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
);
}
}