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

137 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-07-28 15:23:17 +02:00
import { ProviderSlug } from "#/lib/demos";
2023-08-23 08:24:12 +02:00
import { server } from "#/lib/zitadel";
2023-07-31 11:56:23 +02:00
import Alert, { AlertType } from "#/ui/Alert";
2023-07-28 15:23:17 +02:00
import {
AddHumanUserRequest,
IDPInformation,
2023-08-23 08:24:12 +02:00
RetrieveIdentityProviderIntentResponse,
2023-07-28 15:23:17 +02:00
user,
IDPLink,
} from "@zitadel/server";
const PROVIDER_MAPPING: {
[provider: string]: (rI: IDPInformation) => Partial<AddHumanUserRequest>;
} = {
[ProviderSlug.GOOGLE]: (idp: IDPInformation) => {
const idpLink: IDPLink = {
idpId: idp.idpId,
userId: idp.userId,
userName: idp.userName,
};
const req: Partial<AddHumanUserRequest> = {
username: idp.userName,
email: {
email: idp.rawInformation?.User?.email,
isVerified: true,
},
// organisation: Organisation | undefined;
profile: {
displayName: idp.rawInformation?.User?.name ?? "",
2023-08-23 08:24:12 +02:00
givenName: idp.rawInformation?.User?.given_name ?? "",
familyName: idp.rawInformation?.User?.family_name ?? "",
2023-07-28 15:23:17 +02:00
},
idpLinks: [idpLink],
};
return req;
},
[ProviderSlug.GITHUB]: (idp: IDPInformation) => {
const idpLink: IDPLink = {
idpId: idp.idpId,
userId: idp.userId,
userName: idp.userName,
};
const req: Partial<AddHumanUserRequest> = {
username: idp.userName,
email: {
email: idp.rawInformation?.email,
isVerified: true,
},
// organisation: Organisation | undefined;
profile: {
displayName: idp.rawInformation?.name ?? "",
2023-08-23 08:24:12 +02:00
givenName: idp.rawInformation?.name ?? "",
familyName: idp.rawInformation?.name ?? "",
2023-07-28 15:23:17 +02:00
},
idpLinks: [idpLink],
};
return req;
},
};
function retrieveIDP(
id: string,
token: string
): Promise<IDPInformation | undefined> {
const userService = user.getUser(server);
return userService
2023-08-23 08:24:12 +02:00
.retrieveIdentityProviderIntent(
{ idpIntentId: id, idpIntentToken: token },
{}
)
.then((resp: RetrieveIdentityProviderIntentResponse) => {
2023-07-28 15:23:17 +02:00
return resp.idpInformation;
});
}
function createUser(
provider: ProviderSlug,
info: IDPInformation
): Promise<string> {
2023-08-03 09:53:43 +02:00
const userData = PROVIDER_MAPPING[provider](info);
2023-07-28 15:23:17 +02:00
const userService = user.getUser(server);
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 } = searchParams;
const { provider } = params;
if (provider && id && token) {
2023-07-31 11:56:23 +02:00
return retrieveIDP(id, token)
.then((information) => {
if (information) {
return createUser(provider, information).catch((error) => {
throw new Error(error.details);
});
} else {
throw new Error("Could not get user information.");
}
})
.then((userId) => {
return (
<div className="flex flex-col items-center space-y-4">
<h1>Register successful</h1>
<div>You have successfully been registered!</div>
</div>
);
})
.catch((error: Error) => {
return (
<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>
</div>
);
});
2023-07-28 15:23:17 +02:00
} else {
return (
<div className="flex flex-col items-center space-y-4">
2023-07-31 11:56:23 +02:00
<h1>Register</h1>
2023-07-28 15:23:17 +02:00
<p className="ztdl-p">No id and token received!</p>
</div>
);
}
}