feat(oidc): add form validation for oidc settings

pull/2792/head
Jakob Ankarhem 2 years ago committed by Jakob Ankarhem
parent 9277a65e11
commit b0d1be7276
No known key found for this signature in database
GPG Key ID: 149CBB661002B3BE

@ -12,6 +12,7 @@ import { Field, Form, Formik } from 'formik';
import { defineMessages, useIntl } from 'react-intl'; import { defineMessages, useIntl } from 'react-intl';
import { useToasts } from 'react-toast-notifications'; import { useToasts } from 'react-toast-notifications';
import useSWR, { mutate } from 'swr'; import useSWR, { mutate } from 'swr';
import * as yup from 'yup';
const messages = defineMessages({ const messages = defineMessages({
users: 'Users', users: 'Users',
@ -37,7 +38,36 @@ const messages = defineMessages({
oidcDomain: 'OIDC Domain', oidcDomain: 'OIDC Domain',
}); });
const SettingsUsers = () => { const validationSchema = yup.object().shape({
oidcLogin: yup.boolean(),
oidcClientId: yup.string().when('oidcLogin', {
is: true,
then: yup.string().required(),
}),
oidcClientSecret: yup.string().when('oidcLogin', {
is: true,
then: yup.string().required(),
}),
oidcDomain: yup.string().when('oidcLogin', {
is: true,
then: yup
.string()
.required()
.test({
message: 'Must be a valid domain',
test: (val) => {
return (
!!val &&
/^(?!:\/\/)([a-zA-Z0-9-_]+\.)*[a-zA-Z0-9][a-zA-Z0-9-_]+\.[a-zA-Z]{2,11}?$/.test(
val
)
);
},
}),
}),
});
const SettingsUsers: React.FC = () => {
const { addToast } = useToasts(); const { addToast } = useToasts();
const intl = useIntl(); const intl = useIntl();
const { const {
@ -80,6 +110,7 @@ const SettingsUsers = () => {
tvQuotaDays: data?.defaultQuotas.tv.quotaDays ?? 7, tvQuotaDays: data?.defaultQuotas.tv.quotaDays ?? 7,
defaultPermissions: data?.defaultPermissions ?? 0, defaultPermissions: data?.defaultPermissions ?? 0,
}} }}
validationSchema={validationSchema}
enableReinitialize enableReinitialize
onSubmit={async (values) => { onSubmit={async (values) => {
try { try {
@ -119,7 +150,7 @@ const SettingsUsers = () => {
} }
}} }}
> >
{({ isSubmitting, values, setFieldValue }) => { {({ isSubmitting, values, setFieldValue, errors, touched }) => {
return ( return (
<Form className="section"> <Form className="section">
<div className="form-row"> <div className="form-row">
@ -203,6 +234,9 @@ const SettingsUsers = () => {
type="text" type="text"
/> />
</div> </div>
{errors.oidcDomain && touched.oidcDomain && (
<div className="error">{errors.oidcDomain}</div>
)}
</div> </div>
</div> </div>
<div className="form-row"> <div className="form-row">
@ -217,6 +251,9 @@ const SettingsUsers = () => {
type="text" type="text"
/> />
</div> </div>
{errors.oidcClientId && touched.oidcDomain && (
<div className="error">{errors.oidcClientId}</div>
)}
</div> </div>
</div> </div>
<div className="form-row"> <div className="form-row">
@ -236,6 +273,12 @@ const SettingsUsers = () => {
autoComplete="off" autoComplete="off"
/> />
</div> </div>
{errors.oidcClientSecret &&
touched.oidcClientSecret && (
<div className="error">
{errors.oidcClientSecret}
</div>
)}
</div> </div>
</div> </div>
</> </>

Loading…
Cancel
Save