- handle form save for jwtTTL
- update validation for advanced settings page black list some unused fields which causing errors
This commit is contained in:
1 parent
a4344b89a2
commit
57edff36c9
3 files changed
+50
-23
No files matched your search
@@ -35,18 +35,30 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
systemEmailPort: "",
|
systemEmailPort: "",
|
||||||
systemEmailAddress: "",
|
systemEmailAddress: "",
|
||||||
systemEmailPassword: "",
|
systemEmailPassword: "",
|
||||||
jwtTTL: "",
|
jwtTTLNum: 99,
|
||||||
jwtTTLUnits: "days",
|
jwtTTLUnits: "days",
|
||||||
|
jwtTTL: "99d",
|
||||||
dbType: "",
|
dbType: "",
|
||||||
redisHost: "",
|
redisHost: "",
|
||||||
redisPort: "",
|
redisPort: "",
|
||||||
pagespeedApiKey: "",
|
pagespeedApiKey: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const parseJWTTTL = (data) => {
|
||||||
|
if (data.jwtTTL) {
|
||||||
|
const len = data.jwtTTL.length;
|
||||||
|
data.jwtTTLNum = data.jwtTTL.substring(0, len - 1);
|
||||||
|
data.jwtTTLUnits = unitItems.filter(
|
||||||
|
(itm) => itm._id == data.jwtTTL.substring(len - 1)
|
||||||
|
)[0].name;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const getSettings = async () => {
|
const getSettings = async () => {
|
||||||
const action = await dispatch(getAppSettings({ authToken }));
|
const action = await dispatch(getAppSettings({ authToken }));
|
||||||
if (action.payload.success) {
|
if (action.payload.success) {
|
||||||
|
parseJWTTTL(action.payload.data)
|
||||||
setLocalSettings(action.payload.data);
|
setLocalSettings(action.payload.data);
|
||||||
} else {
|
} else {
|
||||||
createToast({ body: "Failed to get settings" });
|
createToast({ body: "Failed to get settings" });
|
||||||
@@ -70,15 +82,13 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const unitItems = [
|
const unitItems = [
|
||||||
{ _id: 1, name: "days" },
|
{ _id: "d", name: "days" },
|
||||||
{ _id: 2, name: "weeks" },
|
{ _id: "h", name: "hours" }
|
||||||
{ _id: 3, name: "months" }
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const unitItemLookup = {
|
const unitItemLookup = {
|
||||||
days: 1,
|
days: "d",
|
||||||
weeks: 2,
|
hours: "h",
|
||||||
months: 3,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLogLevel = (e) => {
|
const handleLogLevel = (e) => {
|
||||||
@@ -111,14 +121,17 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
if (hasValidationErrors(localSettings, advancedSettingsValidation, setErrors))
|
localSettings.jwtTTL =
|
||||||
|
localSettings.jwtTTLNum + unitItemLookup[localSettings.jwtTTLUnits];
|
||||||
|
if (hasValidationErrors(localSettings, advancedSettingsValidation, setErrors)) {
|
||||||
return;
|
return;
|
||||||
const action = await dispatch(
|
}
|
||||||
|
const action = await dispatch(
|
||||||
updateAppSettings({ settings: localSettings, authToken })
|
updateAppSettings({ settings: localSettings, authToken })
|
||||||
);
|
);
|
||||||
let body = "";
|
let body = "";
|
||||||
if (action.payload.success) {
|
if (action.payload.success) {
|
||||||
console.log(action.payload.data);
|
parseJWTTTL(action.payload.data)
|
||||||
setLocalSettings(action.payload.data);
|
setLocalSettings(action.payload.data);
|
||||||
body = "Settings saved successfully";
|
body = "Settings saved successfully";
|
||||||
} else {
|
} else {
|
||||||
@@ -228,13 +241,13 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
<Stack gap={theme.spacing(20)}>
|
<Stack gap={theme.spacing(20)}>
|
||||||
<Stack direction="row" gap={theme.spacing(10)} ><Field
|
<Stack direction="row" gap={theme.spacing(10)} ><Field
|
||||||
type="number"
|
type="number"
|
||||||
id="jwtTTL"
|
id="jwtTTLNum"
|
||||||
label="JWT time to live"
|
label="JWT time to live"
|
||||||
name="jwtTTL"
|
name="jwtTTLNum"
|
||||||
value={localSettings.jwtTTL}
|
value={localSettings.jwtTTLNum.toString()}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
onBlur={handleBlur}
|
onBlur={handleBlur}
|
||||||
error={errors.jwtTTL}
|
error={errors.jwtTTLNum}
|
||||||
/>
|
/>
|
||||||
<Select
|
<Select
|
||||||
id="jwtTTLUnits"
|
id="jwtTTLUnits"
|
||||||
@@ -246,7 +259,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
value={unitItemLookup[localSettings.jwtTTLUnits]}
|
value={unitItemLookup[localSettings.jwtTTLUnits]}
|
||||||
onChange={handleJWTTTLUnits}
|
onChange={handleJWTTTLUnits}
|
||||||
onBlur={handleBlur}
|
onBlur={handleBlur}
|
||||||
error={errors.jwtttlUnits}
|
error={errors.jwtTTLUnits}
|
||||||
/></Stack>
|
/></Stack>
|
||||||
<Field
|
<Field
|
||||||
type="text"
|
type="text"
|
||||||
|
|||||||
@@ -15,10 +15,22 @@ const hasValidationErrors = (form, validation, setErrors) => {
|
|||||||
if (error) {
|
if (error) {
|
||||||
const newErrors = {};
|
const newErrors = {};
|
||||||
error.details.forEach((err) => {
|
error.details.forEach((err) => {
|
||||||
newErrors[err.path[0]] = err.message?? "Validation error";
|
if (
|
||||||
|
![
|
||||||
|
"clientHost",
|
||||||
|
"refreshTokenSecret",
|
||||||
|
"dbConnectionString",
|
||||||
|
"refreshTokenTTL",
|
||||||
|
"jwtTTL",
|
||||||
|
].includes(err.path[0])
|
||||||
|
) {
|
||||||
|
newErrors[err.path[0]] = err.message ?? "Validation error";
|
||||||
|
}
|
||||||
});
|
});
|
||||||
setErrors(newErrors);
|
if (Object.keys(newErrors).length > 0) {
|
||||||
return true;
|
setErrors(newErrors);
|
||||||
|
return true;
|
||||||
|
} else return false;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -138,18 +138,20 @@ const advancedSettingsValidation = joi.object({
|
|||||||
systemEmailPort: joi.string().allow(""),
|
systemEmailPort: joi.string().allow(""),
|
||||||
systemEmailAddress: joi.string().allow(""),
|
systemEmailAddress: joi.string().allow(""),
|
||||||
systemEmailPassword: joi.string().allow(""),
|
systemEmailPassword: joi.string().allow(""),
|
||||||
jwtTTL: joi.string().trim().messages({
|
jwtTTLNum: joi
|
||||||
"string.empty": "JWT TTL is required.",
|
.number()
|
||||||
}),
|
.messages({
|
||||||
|
"any.required": "JWT TTL is required.",
|
||||||
|
}),
|
||||||
jwtTTLUnits: joi
|
jwtTTLUnits: joi
|
||||||
.string()
|
.string()
|
||||||
.trim()
|
.trim()
|
||||||
.custom((value, helpers) => {
|
.custom((value, helpers) => {
|
||||||
if (value == 0) {
|
if( !["days", "hours"].includes(value)) {
|
||||||
return helpers.message("JWT TTL unit is required.");
|
return helpers.message("JWT TTL unit is required.");
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}),
|
}),
|
||||||
dbType: joi.string().trim().messages({
|
dbType: joi.string().trim().messages({
|
||||||
"string.empty": "DB type is required.",
|
"string.empty": "DB type is required.",
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in new issue
Block a user