import { ProviderSlug } from "#/lib/demos"; import { server } from "#/lib/zitadel"; import Alert, { AlertType } from "#/ui/Alert"; import IdpSignin from "#/ui/IdpSignin"; import { createSessionForIdpAndUpdateCookie } from "#/utils/session"; import { AddHumanUserRequest, IDPInformation, RetrieveIdentityProviderIntentResponse, user, IDPLink, Session, } from "@zitadel/server"; import { ClientError } from "nice-grpc"; const PROVIDER_MAPPING: { [provider: string]: (rI: IDPInformation) => Partial; } = { [ProviderSlug.GOOGLE]: (idp: IDPInformation) => { const idpLink: IDPLink = { idpId: idp.idpId, userId: idp.userId, userName: idp.userName, }; const req: Partial = { username: idp.userName, email: { email: idp.rawInformation?.User?.email, isVerified: true, }, // organisation: Organisation | undefined; profile: { displayName: idp.rawInformation?.User?.name ?? "", givenName: idp.rawInformation?.User?.given_name ?? "", familyName: idp.rawInformation?.User?.family_name ?? "", }, idpLinks: [idpLink], }; return req; }, [ProviderSlug.GITHUB]: (idp: IDPInformation) => { const idpLink: IDPLink = { idpId: idp.idpId, userId: idp.userId, userName: idp.userName, }; const req: Partial = { username: idp.userName, email: { email: idp.rawInformation?.email, isVerified: true, }, // organisation: Organisation | undefined; profile: { displayName: idp.rawInformation?.name ?? "", givenName: idp.rawInformation?.name ?? "", familyName: idp.rawInformation?.name ?? "", }, idpLinks: [idpLink], }; return req; }, }; function retrieveIDPIntent( id: string, token: string ): Promise { const userService = user.getUser(server); return userService.retrieveIdentityProviderIntent( { idpIntentId: id, idpIntentToken: token }, {} ); } function createUser( provider: ProviderSlug, info: IDPInformation ): Promise { const userData = PROVIDER_MAPPING[provider](info); const userService = user.getUser(server); return userService.addHumanUser(userData, {}).then((resp) => resp.userId); } export default async function Page({ searchParams, params, }: { searchParams: Record; params: { provider: ProviderSlug }; }) { const { id, token } = searchParams; const { provider } = params; 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: ClientError) => { 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!

); } }