import { ProviderSlug } from "@/lib/demos"; import { getBrandingSettings, userService } from "@/lib/zitadel"; import Alert, { AlertType } from "@/ui/Alert"; import DynamicTheme from "@/ui/DynamicTheme"; import IdpSignin from "@/ui/IdpSignin"; import { AddHumanUserRequest } from "@zitadel/proto/zitadel/user/v2beta/user_service_pb"; import { IDPInformation, IDPLink, } from "@zitadel/proto/zitadel/user/v2beta/idp_pb"; import { PartialMessage } from "@zitadel/client2"; const PROVIDER_MAPPING: { [provider: string]: ( rI: IDPInformation, ) => PartialMessage; } = { [ProviderSlug.GOOGLE]: (idp: IDPInformation) => { const rawInfo = idp.rawInformation?.toJson() as { User: { email: string; name?: string; given_name?: string; family_name?: string; }; }; const idpLink: PartialMessage = { idpId: idp.idpId, userId: idp.userId, userName: idp.userName, }; const req: PartialMessage = { 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.GITHUB]: (idp: IDPInformation) => { const rawInfo = idp.rawInformation?.toJson() as { email: string; name: string; }; const idpLink: PartialMessage = { idpId: idp.idpId, userId: idp.userId, userName: idp.userName, }; const req: PartialMessage = { username: idp.userName, email: { email: rawInfo?.email, verification: { case: "isVerified", value: true }, }, // organisation: Organisation | undefined; profile: { displayName: rawInfo?.name ?? "", givenName: rawInfo?.name ?? "", familyName: rawInfo?.name ?? "", }, idpLinks: [idpLink], }; return req; }, }; function retrieveIDPIntent(id: string, token: string) { return userService.retrieveIdentityProviderIntent( { idpIntentId: id, idpIntentToken: token }, {}, ); } function createUser( provider: ProviderSlug, info: IDPInformation, ): Promise { const userData = PROVIDER_MAPPING[provider](info); return userService.addHumanUser(userData, {}).then((resp) => resp.userId); } export default async function Page({ searchParams, params, }: { searchParams: Record; params: { provider: ProviderSlug }; }) { const { id, token, authRequestId, organization } = searchParams; const { provider } = params; const branding = await getBrandingSettings(organization); if (provider && id && token) { return retrieveIDPIntent(id, token) .then((resp) => { const { idpInformation, userId } = resp; if (idpInformation) { // handle login if (userId) { return (

Login successful

You have successfully been loggedIn!
); } else { // handle register return createUser(provider, idpInformation) .then((userId) => { return (

Register successful

You have successfully been registered!
); }) .catch((error) => { return (

Register failed

{ {JSON.stringify(error.message)} }
); }); } } else { throw new Error("Could not get user information."); } }) .catch((error) => { return (

An error occurred

{ {JSON.stringify(error.message)} }
); }); } else { return (

Register

No id and token received!

); } }