Skip to content

Commit cfa8900

Browse files
author
Kerwin
committed
fix: 邮箱格式校验
1 parent 562b089 commit cfa8900

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

service/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { ChatOptions, Config, MailConfig, SiteConfig, UserInfo } from './st
99
import { Status } from './storage/model'
1010
import { clearChat, createChatRoom, createUser, deleteAllChatRooms, deleteChat, deleteChatRoom, existsChatRoom, getChat, getChatRooms, getChats, getUser, getUserById, insertChat, renameChatRoom, updateChat, updateConfig, updateUserInfo, verifyUser } from './storage/mongo'
1111
import { limiter } from './middleware/limiter'
12-
import { isNotEmptyString } from './utils/is'
12+
import { isEmail, isNotEmptyString } from './utils/is'
1313
import { sendTestMail, sendVerifyMail } from './utils/mail'
1414
import { checkUserVerify, getUserVerifyUrl, md5 } from './utils/security'
1515
import { rootAuth } from './middleware/rootAuth'
@@ -245,6 +245,10 @@ router.post('/user-register', async (req, res) => {
245245
res.send({ status: 'Fail', message: '注册账号功能未启用 | Register account is disabled!', data: null })
246246
return
247247
}
248+
if (!isEmail(username)) {
249+
res.send({ status: 'Fail', message: '请输入正确的邮箱 | Please enter a valid email address.', data: null })
250+
return
251+
}
248252
if (isNotEmptyString(config.siteConfig.registerMails)) {
249253
let allowSuffix = false
250254
const emailSuffixs = config.siteConfig.registerMails.split(',')
@@ -312,7 +316,7 @@ router.post('/session', async (req, res) => {
312316
router.post('/user-login', async (req, res) => {
313317
try {
314318
const { username, password } = req.body as { username: string; password: string }
315-
if (!username || !password)
319+
if (!username || !password || !isEmail(username))
316320
throw new Error('用户名或密码为空 | Username or password is empty')
317321

318322
const user = await getUser(username)

service/src/utils/is.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ export function isBoolean<T extends boolean>(value: T | unknown): value is boole
1717
export function isFunction<T extends (...args: any[]) => any | void | never>(value: T | unknown): value is T {
1818
return Object.prototype.toString.call(value) === '[object Function]'
1919
}
20+
21+
export function isEmail(value: any): boolean {
22+
return isNotEmptyString(value) && /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(value)
23+
}

0 commit comments

Comments
 (0)