import { Alert, AlertType } from "@/components/alert"; import { DynamicTheme } from "@/components/dynamic-theme"; import { IdpSignin } from "@/components/idp-signin"; import { idpTypeToIdentityProviderType, PROVIDER_MAPPING } from "@/lib/idp"; import { addIDPLink, createUser, getBrandingSettings, getIDPByID, listUsers, retrieveIDPIntent, } from "@/lib/zitadel"; import { AutoLinkingOption } from "@zitadel/proto/zitadel/idp/v2/idp_pb"; export default async function Page({ searchParams, params, }: { searchParams: Record; params: { provider: string }; }) { 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 return (

Login successful

You have successfully been loggedIn!
); } if (idpInformation) { const idp = await getIDPByID(idpInformation.idpId); const options = idp?.config?.options; if (!idp) { throw new Error("IDP not found"); } const providerType = idpTypeToIdentityProviderType(idp.type); // search for potential user via username, then link if (options?.isLinkingAllowed) { let foundUser; const email = PROVIDER_MAPPING[providerType](idpInformation).email?.email; 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; }); } if (foundUser) { const idpLink = await addIDPLink( { id: idpInformation.idpId, userId: idpInformation.userId, userName: idpInformation.userName, }, foundUser.userId, ).catch((error) => { return (

Linking failed

{ {JSON.stringify(error.message)} }
); }); if (idpLink) { return ( // TODO: possibily login user now

Account successfully linked

Your account has successfully been linked!
); } } } if (options?.isCreationAllowed && options.isAutoCreation) { const newUser = await createUser(providerType, idpInformation); if (newUser) { return (

Register successful

You have successfully been registered!
); } } // return login failed if no linking or creation is allowed and no user was found return (

Login failed

{ User could not be logged in }
); } else { return (

Login failed

{ Could not get user information }
); } }) .catch((error) => { return (

An error occurred

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

Register

No id and token received!

); } }