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

190 lines
5.6 KiB
TypeScript
Raw Normal View History

2024-05-13 16:17:12 -04:00
import { ProviderSlug } from "@/lib/demos";
import { getBrandingSettings, userService } 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";
import { AddHumanUserRequest } from "@zitadel/proto/zitadel/user/v2beta/user_service_pb";
2023-07-28 15:23:17 +02:00
import {
IDPInformation,
IDPLink,
} from "@zitadel/proto/zitadel/user/v2beta/idp_pb";
import { PartialMessage } from "@zitadel/client2";
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 {
User: {
email: string;
name?: string;
given_name?: string;
family_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.User?.email,
verification: { case: "isVerified", value: true },
2023-07-28 15:23:17 +02:00
},
// organisation: Organisation | undefined;
profile: {
displayName: rawInfo.User?.name ?? "",
givenName: rawInfo.User?.given_name ?? "",
familyName: rawInfo.User?.family_name ?? "",
2023-07-28 15:23:17 +02:00
},
idpLinks: [idpLink],
};
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;
},
};
function retrieveIDPIntent(id: string, token: string) {
2024-03-19 14:15:54 +01:00
return userService.retrieveIdentityProviderIntent(
{ idpIntentId: id, idpIntentToken: token },
2024-05-13 16:17:12 -04:00
{},
2024-03-19 14:15:54 +01:00
);
2023-07-28 15:23:17 +02:00
}
function createUser(
provider: ProviderSlug,
2024-05-13 16:17:12 -04:00
info: IDPInformation,
2023-07-28 15:23:17 +02:00
): Promise<string> {
2023-08-03 09:53:43 +02:00
const userData = PROVIDER_MAPPING[provider](info);
2023-07-28 15:23:17 +02:00
return userService.addHumanUser(userData, {}).then((resp) => resp.userId);
}
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)
.then((resp) => {
const { idpInformation, userId } = resp;
2024-05-03 10:09:18 +02:00
2024-03-19 14:15:54 +01:00
if (idpInformation) {
2024-03-15 17:21:21 +01:00
// handle login
2024-03-19 14:15:54 +01:00
if (userId) {
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>
2024-03-19 14:15:54 +01:00
<IdpSignin
userId={userId}
idpIntent={{ idpIntentId: id, idpIntentToken: token }}
authRequestId={authRequestId}
/>
</div>
</DynamicTheme>
2024-03-19 14:15:54 +01:00
);
2024-03-15 17:21:21 +01:00
} else {
// handle register
2024-03-19 14:15:54 +01:00
return createUser(provider, idpInformation)
2024-03-15 17:21:21 +01:00
.then((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
);
})
.catch((error) => {
2024-03-19 14:15:54 +01:00
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">
<h1>Register failed</h1>
<div className="w-full">
{
<Alert type={AlertType.ALERT}>
{JSON.stringify(error.message)}
</Alert>
}
</div>
2024-03-19 14:15:54 +01:00
</div>
</DynamicTheme>
2024-03-19 14:15:54 +01:00
);
2024-03-15 17:21:21 +01:00
});
}
2023-07-31 11:56:23 +02:00
} else {
throw new Error("Could not get user information.");
}
})
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
);
}
}