- Update jwt ttl validation
This commit is contained in:
1 parent
4d8be0e2d6
commit
4966f3fd38
3 files changed
+57
-40
No files matched your search
@@ -12,6 +12,8 @@ import { useNavigate } from "react-router";
|
||||
import { getAppSettings, updateAppSettings } from "../../Features/Settings/settingsSlice";
|
||||
import { useState, useEffect } from "react";
|
||||
import Select from "../../Components/Inputs/Select";
|
||||
import { advancedSettingsValidation } from "../../Validation/validation";
|
||||
import { buildErrors, hasValidationErrors } from "../../Validation/error";
|
||||
|
||||
const AdvancedSettings = ({ isAdmin }) => {
|
||||
const navigate = useNavigate();
|
||||
@@ -21,7 +23,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
navigate("/");
|
||||
}
|
||||
}, [navigate, isAdmin]);
|
||||
|
||||
const [errors, setErrors] = useState({});
|
||||
const theme = useTheme();
|
||||
const { authToken } = useSelector((state) => state.auth);
|
||||
const dispatch = useDispatch();
|
||||
@@ -75,9 +77,20 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
const handleChange = (event) => {
|
||||
const { value, id } = event.target;
|
||||
setLocalSettings({ ...localSettings, [id]: value });
|
||||
const { error } = advancedSettingsValidation.validate(
|
||||
{ [id]: value },
|
||||
{
|
||||
abortEarly: false,
|
||||
}
|
||||
);
|
||||
setErrors((prev) => {
|
||||
return buildErrors(prev, id, error);
|
||||
});
|
||||
};
|
||||
|
||||
const handleSave = async () => {
|
||||
if (hasValidationErrors(localSettings, advancedSettingsValidation, setErrors))
|
||||
return;
|
||||
const action = await dispatch(
|
||||
updateAppSettings({ settings: localSettings, authToken })
|
||||
);
|
||||
@@ -118,6 +131,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
label="API URL Host"
|
||||
value={localSettings.apiBaseUrl}
|
||||
onChange={handleChange}
|
||||
error={errors.apiBaseUrl}
|
||||
/>
|
||||
<Select
|
||||
id="logLevel"
|
||||
@@ -126,6 +140,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
items={logItems}
|
||||
value={logItemLookup[localSettings.logLevel]}
|
||||
onChange={handleLogLevel}
|
||||
error={errors.logLevel}
|
||||
/>
|
||||
</Stack>
|
||||
</ConfigBox>
|
||||
@@ -145,6 +160,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="systemEmailHost"
|
||||
value={localSettings.systemEmailHost}
|
||||
onChange={handleChange}
|
||||
error={errors.systemEmailHost}
|
||||
/>
|
||||
<Field
|
||||
type="number"
|
||||
@@ -153,6 +169,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="systemEmailPort"
|
||||
value={localSettings.systemEmailPort.toString()}
|
||||
onChange={handleChange}
|
||||
error={errors.systemEmailPort}
|
||||
/>
|
||||
<Field
|
||||
type="email"
|
||||
@@ -161,6 +178,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="systemEmailAddress"
|
||||
value={localSettings.systemEmailAddress}
|
||||
onChange={handleChange}
|
||||
error={errors.systemEmailAddress}
|
||||
/>
|
||||
<Field
|
||||
type="text"
|
||||
@@ -169,6 +187,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="systemEmailPassword"
|
||||
value={localSettings.systemEmailPassword}
|
||||
onChange={handleChange}
|
||||
error={errors.systemEmailPassword}
|
||||
/>
|
||||
</Stack>
|
||||
</ConfigBox>
|
||||
@@ -187,6 +206,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="jwtTTL"
|
||||
value={localSettings.jwtTTL}
|
||||
onChange={handleChange}
|
||||
error={errors.jwtTTL}
|
||||
/>
|
||||
<Field
|
||||
type="text"
|
||||
@@ -195,6 +215,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="dbType"
|
||||
value={localSettings.dbType}
|
||||
onChange={handleChange}
|
||||
error={errors.dbType}
|
||||
/>
|
||||
<Field
|
||||
type="text"
|
||||
@@ -203,6 +224,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="redisHost"
|
||||
value={localSettings.redisHost}
|
||||
onChange={handleChange}
|
||||
error={errors.redisHost}
|
||||
/>
|
||||
<Field
|
||||
type="number"
|
||||
@@ -211,6 +233,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="redisPort"
|
||||
value={localSettings.redisPort.toString()}
|
||||
onChange={handleChange}
|
||||
error={errors.redisPort}
|
||||
/>
|
||||
<Field
|
||||
type="text"
|
||||
@@ -219,6 +242,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="pagespeedApiKey"
|
||||
value={localSettings.pagespeedApiKey}
|
||||
onChange={handleChange}
|
||||
error={errors.pagespeedApiKey}
|
||||
/>
|
||||
</Stack>
|
||||
</ConfigBox>
|
||||
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
MS_PER_WEEK,
|
||||
} from "../../../Utils/timeUtils";
|
||||
import { useNavigate, useParams } from "react-router-dom";
|
||||
import { buildErrors, hasValidationErrors } from "../../../Validation/error";
|
||||
|
||||
const getDurationAndUnit = (durationInMs) => {
|
||||
if (durationInMs % MS_PER_DAY === 0) {
|
||||
@@ -176,16 +177,6 @@ const CreateMaintenance = () => {
|
||||
fetchMonitors();
|
||||
}, [authToken, user]);
|
||||
|
||||
const buildErrors = (prev, id, error) => {
|
||||
const updatedErrors = { ...prev };
|
||||
if (error) {
|
||||
updatedErrors[id] = error.details[0].message;
|
||||
} else {
|
||||
delete updatedErrors[id];
|
||||
}
|
||||
return updatedErrors;
|
||||
};
|
||||
|
||||
const handleSearch = (value) => {
|
||||
setSearch(value);
|
||||
};
|
||||
@@ -224,20 +215,8 @@ const CreateMaintenance = () => {
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
const { error } = maintenanceWindowValidation.validate(form, {
|
||||
abortEarly: false,
|
||||
});
|
||||
|
||||
// If errors, return early
|
||||
if (error) {
|
||||
const newErrors = {};
|
||||
error.details.forEach((err) => {
|
||||
newErrors[err.path[0]] = err.message;
|
||||
});
|
||||
setErrors(newErrors);
|
||||
logger.error(error);
|
||||
return;
|
||||
}
|
||||
if (hasValidationErrors(form, maintenanceWindowValidation, setErrors))
|
||||
return;
|
||||
// Build timestamp for maintenance window from startDate and startTime
|
||||
const start = dayjs(form.startDate)
|
||||
.set("hour", form.startTime.hour())
|
||||
|
||||
@@ -134,30 +134,44 @@ const advancedSettingsValidation = joi.object({
|
||||
"string.uri": "The URL you provided is not valid.",
|
||||
}),
|
||||
logLevel: joi.string().valid("debug", "none", "error", "warn").allow(""),
|
||||
systemEmailHost: joi.string().allow(''),
|
||||
systemEmailPort: joi.string().allow(''),
|
||||
systemEmailAddress: joi
|
||||
.string().allow(''),
|
||||
systemEmailPassword: joi.string().allow(''),
|
||||
systemEmailHost: joi.string().allow(""),
|
||||
systemEmailPort: joi.string().allow(""),
|
||||
systemEmailAddress: joi.string().allow(""),
|
||||
systemEmailPassword: joi.string().allow(""),
|
||||
jwtTTL: joi
|
||||
.string()
|
||||
.trim()
|
||||
.messages({
|
||||
"string.empty": "JWT TTL is required."
|
||||
"string.empty": "JWT TTL is required.",
|
||||
})
|
||||
.custom((value, helpers) => {
|
||||
// test against regx start with a number followed by number or .,
|
||||
//ends with d or h
|
||||
const regex = /^[1-9]+\d*(.)*(d|h)$/;
|
||||
const found = value.match(regex);
|
||||
if (!found) {
|
||||
return helpers.message(
|
||||
"JWT TTL should start with a non zero number and ends with 'd' or 'h'"
|
||||
);
|
||||
}
|
||||
return value;
|
||||
}),
|
||||
dbType: joi.string().trim().messages({
|
||||
"string.empty": "DB type is required",
|
||||
"string.empty": "DB type is required.",
|
||||
}),
|
||||
redisHost: joi.string().trim().messages({
|
||||
"string.empty": "Redis host is required",
|
||||
"string.empty": "Redis host is required.",
|
||||
}),
|
||||
redisPort: joi.string().allow('').custom((value, helpers) => {
|
||||
if(value && Number.isNaN(parseInt(value))){
|
||||
return helpers.message("Redis port must be a number")
|
||||
}
|
||||
return value
|
||||
}),
|
||||
pagespeedApiKey: joi.string().allow('')
|
||||
redisPort: joi
|
||||
.string()
|
||||
.allow("")
|
||||
.custom((value, helpers) => {
|
||||
if (value && Number.isNaN(parseInt(value))) {
|
||||
return helpers.message("Redis port must be a number.");
|
||||
}
|
||||
return value;
|
||||
}),
|
||||
pagespeedApiKey: joi.string().allow(""),
|
||||
});
|
||||
|
||||
export {
|
||||
|
||||
Reference in new issue
Block a user