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