misc: added frontend validation for oidc form

This commit is contained in:
Sheen Capadngan
2024-06-20 00:15:18 +08:00
parent 1193ddbed1
commit 7c067551a4
@@ -29,7 +29,8 @@ type Props = {
handlePopUpToggle: (popUpName: keyof UsePopUpState<["addOIDC"]>, state?: boolean) => void; handlePopUpToggle: (popUpName: keyof UsePopUpState<["addOIDC"]>, state?: boolean) => void;
}; };
const schema = z.object({ const schema = z
.object({
configurationType: z.string(), configurationType: z.string(),
issuer: z.string().optional(), issuer: z.string().optional(),
discoveryURL: z.string().optional(), discoveryURL: z.string().optional(),
@@ -40,6 +41,54 @@ const schema = z.object({
clientId: z.string().min(1), clientId: z.string().min(1),
clientSecret: z.string().min(1), clientSecret: z.string().min(1),
allowedEmailDomains: z.string().optional() allowedEmailDomains: z.string().optional()
})
.superRefine((data, ctx) => {
if (data.configurationType === ConfigurationType.CUSTOM) {
if (!data.issuer) {
ctx.addIssue({
path: ["issuer"],
message: "Issuer is required",
code: z.ZodIssueCode.custom
});
}
if (!data.authorizationEndpoint) {
ctx.addIssue({
path: ["authorizationEndpoint"],
message: "Authorization endpoint is required",
code: z.ZodIssueCode.custom
});
}
if (!data.jwksUri) {
ctx.addIssue({
path: ["jwksUri"],
message: "JWKS URI is required",
code: z.ZodIssueCode.custom
});
}
if (!data.tokenEndpoint) {
ctx.addIssue({
path: ["tokenEndpoint"],
message: "Token endpoint is required",
code: z.ZodIssueCode.custom
});
}
if (!data.userinfoEndpoint) {
ctx.addIssue({
path: ["userinfoEndpoint"],
message: "Userinfo endpoint is required",
code: z.ZodIssueCode.custom
});
}
} else {
// eslint-disable-next-line no-lonely-if
if (!data.discoveryURL) {
ctx.addIssue({
path: ["discoveryURL"],
message: "Discovery URL is required",
code: z.ZodIssueCode.custom
});
}
}
}); });
export type OIDCFormData = z.infer<typeof schema>; export type OIDCFormData = z.infer<typeof schema>;
@@ -88,7 +137,9 @@ export const OIDCModal = ({ popUp, handlePopUpClose, handlePopUpToggle }: Props)
clientSecret clientSecret
}: OIDCFormData) => { }: OIDCFormData) => {
try { try {
if (!currentOrg) return; if (!currentOrg) {
return;
}
if (!data) { if (!data) {
await createMutateAsync({ await createMutateAsync({