2024-05-13 16:17:12 -04:00
|
|
|
import { ProviderSlug } from "@/lib/demos";
|
2024-04-07 03:56:46 -04:00
|
|
|
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";
|
2024-04-07 03:56:46 -04:00
|
|
|
import { AddHumanUserRequest } from "@zitadel/proto/zitadel/user/v2beta/user_service_pb";
|
2023-07-28 15:23:17 +02:00
|
|
|
import {
|
|
|
|
|
IDPInformation,
|
|
|
|
|
IDPLink,
|
2024-04-07 03:56:46 -04:00
|
|
|
} from "@zitadel/proto/zitadel/user/v2beta/idp_pb";
|
|
|
|
|
import { PartialMessage } from "@zitadel/client2";
|
2023-07-28 15:23:17 +02:00
|
|
|
|
|
|
|
|
const PROVIDER_MAPPING: {
|
2024-04-07 03:56:46 -04:00
|
|
|
[provider: string]: (
|
|
|
|
|
rI: IDPInformation,
|
|
|
|
|
) => PartialMessage<AddHumanUserRequest>;
|
2023-07-28 15:23:17 +02:00
|
|
|
} = {
|
|
|
|
|
[ProviderSlug.GOOGLE]: (idp: IDPInformation) => {
|
2024-04-07 03:56:46 -04:00
|
|
|
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,
|
|
|
|
|
};
|
2024-04-07 03:56:46 -04:00
|
|
|
|
|
|
|
|
const req: PartialMessage<AddHumanUserRequest> = {
|
2023-07-28 15:23:17 +02:00
|
|
|
username: idp.userName,
|
|
|
|
|
email: {
|
2024-04-07 03:56:46 -04:00
|
|
|
email: rawInfo.User?.email,
|
|
|
|
|
verification: { case: "isVerified", value: true },
|
2023-07-28 15:23:17 +02:00
|
|
|
},
|
|
|
|
|
// organisation: Organisation | undefined;
|
|
|
|
|
profile: {
|
2024-04-07 03:56:46 -04:00
|
|
|
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) => {
|
2024-04-07 03:56:46 -04:00
|
|
|
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,
|
|
|
|
|
};
|
2024-04-07 03:56:46 -04:00
|
|
|
const req: PartialMessage<AddHumanUserRequest> = {
|
2023-07-28 15:23:17 +02:00
|
|
|
username: idp.userName,
|
|
|
|
|
email: {
|
2024-04-07 03:56:46 -04:00
|
|
|
email: rawInfo?.email,
|
|
|
|
|
verification: { case: "isVerified", value: true },
|
2023-07-28 15:23:17 +02:00
|
|
|
},
|
|
|
|
|
// organisation: Organisation | undefined;
|
|
|
|
|
profile: {
|
2024-04-07 03:56:46 -04:00
|
|
|
displayName: rawInfo?.name ?? "",
|
|
|
|
|
givenName: rawInfo?.name ?? "",
|
|
|
|
|
familyName: rawInfo?.name ?? "",
|
2023-07-28 15:23:17 +02:00
|
|
|
},
|
|
|
|
|
idpLinks: [idpLink],
|
|
|
|
|
};
|
|
|
|
|
return req;
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-07 03:56:46 -04:00
|
|
|
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 };
|
|
|
|
|
}) {
|
2024-04-01 13:57:39 +02:00
|
|
|
const { id, token, authRequestId, organization } = searchParams;
|
2023-07-28 15:23:17 +02:00
|
|
|
const { provider } = params;
|
|
|
|
|
|
2024-04-07 03:56:46 -04:00
|
|
|
const branding = await getBrandingSettings(organization);
|
2024-04-01 13:57:39 +02:00
|
|
|
|
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 (
|
2024-04-01 13:57:39 +02:00
|
|
|
<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
|
|
|
|
2024-04-01 13:57:39 +02: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 (
|
2024-04-01 13:57:39 +02:00
|
|
|
<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-04-07 03:56:46 -04:00
|
|
|
.catch((error) => {
|
2024-03-19 14:15:54 +01:00
|
|
|
return (
|
2024-04-01 13:57:39 +02:00
|
|
|
<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}>
|
2024-04-07 03:56:46 -04:00
|
|
|
{JSON.stringify(error.message)}
|
2024-04-01 13:57:39 +02:00
|
|
|
</Alert>
|
|
|
|
|
}
|
|
|
|
|
</div>
|
2024-03-19 14:15:54 +01:00
|
|
|
</div>
|
2024-04-01 13:57:39 +02:00
|
|
|
</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 (
|
2024-04-01 13:57:39 +02:00
|
|
|
<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>
|
2024-04-01 13:57:39 +02:00
|
|
|
</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
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|