handle errors more gracefully

This commit is contained in:
peintnermax
2024-08-21 16:08:37 +02:00
parent 775ead416c
commit 96ec3162d8
4 changed files with 38 additions and 43 deletions

View File

@@ -115,27 +115,12 @@ export default async function Page({
);
}
}
} else if (options?.isCreationAllowed && options.isAutoCreation) {
const userId = await createUser(provider, idpInformation).catch(
(error) => {
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>
</div>
</DynamicTheme>
);
},
);
if (userId) {
if (options?.isCreationAllowed && options.isAutoCreation) {
const newUser = await createUser(provider, idpInformation);
if (newUser) {
return (
<DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4">

View File

@@ -23,21 +23,23 @@ export function idpTypeToSlug(idpType: IdentityProviderType) {
// this maps the IDPInformation to the AddHumanUserRequest which is used when creating a user or linking a user (email)
// TODO: extend this object from a other file which can be overwritten by customers like map = { ...PROVIDER_MAPPING, ...customerMap }
export const PROVIDER_MAPPING: {
[provider: string]: (
rI: IDPInformation,
) => PartialMessage<AddHumanUserRequest>;
} = {
[idpTypeToSlug(IdentityProviderType.GOOGLE)]: (idp: IDPInformation) => {
const rawInfo = idp.rawInformation?.toJson() as {
export type OIDC_USER = {
User: {
email: string;
name?: string;
given_name?: string;
family_name?: string;
};
};
};
export const PROVIDER_MAPPING: {
[provider: string]: (
rI: IDPInformation,
) => PartialMessage<AddHumanUserRequest>;
} = {
[idpTypeToSlug(IdentityProviderType.GOOGLE)]: (idp: IDPInformation) => {
const rawInfo = idp.rawInformation?.toJson() as OIDC_USER;
console.log(rawInfo);
const idpLink: PartialMessage<IDPLink> = {
idpId: idp.idpId,
userId: idp.userId,
@@ -62,10 +64,16 @@ export const PROVIDER_MAPPING: {
},
[idpTypeToSlug(IdentityProviderType.AZURE_AD)]: (idp: IDPInformation) => {
const rawInfo = idp.rawInformation?.toJson() as {
jobTitle: string;
mail: string;
mobilePhone: string;
preferredLanguage: string;
id: string;
displayName?: string;
givenName?: string;
surname?: string;
officeLocation?: string;
userPrincipalName: string;
};
const idpLink: PartialMessage<IDPLink> = {
@@ -74,16 +82,18 @@ export const PROVIDER_MAPPING: {
userName: idp.userName,
};
console.log(rawInfo, rawInfo.userPrincipalName);
const req: PartialMessage<AddHumanUserRequest> = {
username: idp.userName,
email: {
email: rawInfo?.mail,
email: rawInfo.mail || rawInfo.userPrincipalName || "",
verification: { case: "isVerified", value: true },
},
profile: {
displayName: rawInfo?.displayName ?? "",
givenName: rawInfo?.givenName ?? "",
familyName: rawInfo?.surname ?? "",
displayName: rawInfo.displayName ?? "",
givenName: rawInfo.givenName ?? "",
familyName: rawInfo.surname ?? "",
},
idpLinks: [idpLink],
};
@@ -105,13 +115,13 @@ export const PROVIDER_MAPPING: {
const req: PartialMessage<AddHumanUserRequest> = {
username: idp.userName,
email: {
email: rawInfo?.email,
email: rawInfo.email,
verification: { case: "isVerified", value: true },
},
profile: {
displayName: rawInfo?.name ?? "",
givenName: rawInfo?.name ?? "",
familyName: rawInfo?.name ?? "",
displayName: rawInfo.name ?? "",
givenName: rawInfo.name ?? "",
familyName: rawInfo.name ?? "",
},
idpLinks: [idpLink],
};

View File

@@ -391,12 +391,10 @@ export function addIDPLink(
);
}
export function createUser(
provider: string,
info: IDPInformation,
): Promise<string> {
export function createUser(provider: string, info: IDPInformation) {
const userData = PROVIDER_MAPPING[provider](info);
return userService.addHumanUser(userData, {}).then((resp) => resp.userId);
console.log("ud", userData);
return userService.addHumanUser(userData, {});
}
/**

View File

@@ -42,7 +42,9 @@ export default function UserAvatar({
loginName={loginName ?? ""}
/>
</div>
<span className="ml-4 text-14px">{loginName}</span>
<span className="ml-4 text-14px max-w-[250px] text-ellipsis overflow-hidden">
{loginName}
</span>
<span className="flex-grow"></span>
{showDropdown && (
<Link