From 0a434d3b3a2d1fa296afdd32a0557b04b67a1007 Mon Sep 17 00:00:00 2001 From: t0ng7u Date: Sat, 12 Jul 2025 03:42:05 +0800 Subject: [PATCH] =?UTF-8?q?=E2=8F=B1=EF=B8=8F=20fix(token):=20disallow=20s?= =?UTF-8?q?electing=20expiration=20date=20earlier=20than=20now?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add custom validator to the `expired_time` DatePicker in `EditToken.js` to ensure that selected expiration timestamps are strictly in the future. - Introduce Promise-based validator that: • Rejects invalid date formats with a clear error message • Prevents past or current dates and shows “Expiration time cannot be earlier than the current time!” - Keeps support for the special “never expires” value (-1) - Blocks form submission until a valid future datetime is provided This change prevents accidental creation of already expired tokens and improves overall robustness of token management. --- web/src/i18n/locales/en.json | 1 + web/src/pages/Token/EditToken.js | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index ed9fe09b..2d964d8c 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -512,6 +512,7 @@ "创建新的兑换码": "Create a new redemption code", "未找到所请求的页面": "The requested page was not found", "过期时间格式错误!": "Expiration time format error!", + "过期时间不能早于当前时间!": "Expiration time cannot be earlier than the current time!", "请输入过期时间,格式为 yyyy-MM-dd HH:mm:ss,-1 表示无限制": "Please enter the expiration time, the format is yyyy-MM-dd HH:mm:ss, -1 means no limit", "此项可选,为一个 JSON 文本,键为用户请求的模型名称,值为要替换的模型名称,例如:": "This is optional, it's a JSON text, the key is the model name requested by the user, and the value is the model name to be replaced, for example:", "此项可选,输入镜像站地址,格式为:": "This is optional, enter the mirror site address, the format is:", diff --git a/web/src/pages/Token/EditToken.js b/web/src/pages/Token/EditToken.js index fb8badf7..c2ab5a98 100644 --- a/web/src/pages/Token/EditToken.js +++ b/web/src/pages/Token/EditToken.js @@ -345,7 +345,23 @@ const EditToken = (props) => { label={t('过期时间')} type='dateTime' placeholder={t('请选择过期时间')} - rules={[{ required: true, message: t('请选择过期时间') }]} + rules={[ + { required: true, message: t('请选择过期时间') }, + { + validator: (rule, value) => { + // 允许 -1 表示永不过期,也允许空值在必填校验时被拦截 + if (value === -1 || !value) return Promise.resolve(); + const time = Date.parse(value); + if (isNaN(time)) { + return Promise.reject(t('过期时间格式错误!')); + } + if (time <= Date.now()) { + return Promise.reject(t('过期时间不能早于当前时间!')); + } + return Promise.resolve(); + }, + }, + ]} showClear style={{ width: '100%' }} />