Skip to content

Commit f9f371a

Browse files
committed
fix: password check
1 parent 199f454 commit f9f371a

File tree

5 files changed

+69
-14
lines changed

5 files changed

+69
-14
lines changed

projects/app/src/pageComponents/account/info/UpdatePswModal.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useTranslation } from 'next-i18next';
55
import { useForm } from 'react-hook-form';
66
import { useRequest2 } from '@fastgpt/web/hooks/useRequest';
77
import { updatePasswordByOld } from '@/web/support/user/api';
8-
import { PasswordRule } from '@/web/support/user/login/constants';
8+
import { checkPasswordRule } from '@/web/support/user/login/constants';
99
import { useToast } from '@fastgpt/web/hooks/useToast';
1010

1111
type FormType = {
@@ -70,9 +70,11 @@ const UpdatePswModal = ({ onClose }: { onClose: () => void }) => {
7070
placeholder={t('account_info:password_tip')}
7171
{...register('newPsw', {
7272
required: true,
73-
pattern: {
74-
value: PasswordRule,
75-
message: t('account_info:password_tip')
73+
validate: (val) => {
74+
if (!checkPasswordRule(val)) {
75+
return t('login:password_tip');
76+
}
77+
return true;
7678
}
7779
})}
7880
></Input>

projects/app/src/pageComponents/login/ForgetPasswordForm.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Dispatch } from 'react';
22
import { FormControl, Box, Input, Button } from '@chakra-ui/react';
33
import { useForm } from 'react-hook-form';
4-
import { LoginPageTypeEnum, PasswordRule } from '@/web/support/user/login/constants';
4+
import { LoginPageTypeEnum, checkPasswordRule } from '@/web/support/user/login/constants';
55
import { postFindPassword } from '@/web/support/user/api';
66
import { useSendCode } from '@/web/support/user/hooks/useSendCode';
77
import type { ResLogin } from '@/global/support/api/userRes.d';
@@ -138,9 +138,11 @@ const RegisterForm = ({ setPageType, loginSuccess }: Props) => {
138138
placeholder={t('login:password_tip')}
139139
{...register('password', {
140140
required: true,
141-
pattern: {
142-
value: PasswordRule,
143-
message: t('login:password_tip')
141+
validate: (val) => {
142+
if (!checkPasswordRule(val)) {
143+
return t('login:password_tip');
144+
}
145+
return true;
144146
}
145147
})}
146148
></Input>

projects/app/src/pageComponents/login/RegisterForm.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Dispatch } from 'react';
22
import { FormControl, Box, Input, Button } from '@chakra-ui/react';
33
import { useForm } from 'react-hook-form';
4-
import { LoginPageTypeEnum, PasswordRule } from '@/web/support/user/login/constants';
4+
import { LoginPageTypeEnum, checkPasswordRule } from '@/web/support/user/login/constants';
55
import { postRegister } from '@/web/support/user/api';
66
import { useSendCode } from '@/web/support/user/hooks/useSendCode';
77
import type { ResLogin } from '@/global/support/api/userRes';
@@ -166,9 +166,11 @@ const RegisterForm = ({ setPageType, loginSuccess }: Props) => {
166166
placeholder={t('login:password_tip')}
167167
{...register('password', {
168168
required: true,
169-
pattern: {
170-
value: PasswordRule,
171-
message: t('login:password_tip')
169+
validate: (val) => {
170+
if (!checkPasswordRule(val)) {
171+
return t('login:password_tip');
172+
}
173+
return true;
172174
}
173175
})}
174176
></Input>

projects/app/src/web/support/user/login/constants.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,21 @@ export enum LoginPageTypeEnum {
55
wechat = 'wechat'
66
}
77

8-
export const PasswordRule =
9-
/^(?:(?=.*\d)(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])|(?=.*\d)(?=.*[!@#$%^&*_])|(?=.*[a-z])(?=.*[A-Z])|(?=.*[a-z])(?=.*[!@#$%^&*_])|(?=.*[A-Z])(?=.*[!@#$%^&*_]))[\dA-Za-z!@#$%^&*_]{6,}$/;
8+
export const checkPasswordRule = (password: string) => {
9+
const patterns = [
10+
/\d/, // Contains digits
11+
/[a-z]/, // Contains lowercase letters
12+
/[A-Z]/, // Contains uppercase letters
13+
/[!@#$%^&*_]/ // Contains special characters
14+
];
15+
const validChars = /^[\dA-Za-z!@#$%^&*_]{6,100}$/;
16+
17+
// Check length and valid characters
18+
if (!validChars.test(password)) return false;
19+
20+
// Count how many patterns are satisfied
21+
const matchCount = patterns.filter((pattern) => pattern.test(password)).length;
22+
23+
// Must satisfy at least 2 patterns
24+
return matchCount >= 2;
25+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { checkPasswordRule } from '@/web/support/user/login/constants';
3+
4+
describe('PasswordRule', () => {
5+
it('should be a valid password', () => {
6+
// Small password
7+
expect(checkPasswordRule('123A')).toBe(false);
8+
expect(checkPasswordRule('@ga21')).toBe(false);
9+
10+
// Test single type characters
11+
expect(checkPasswordRule('123456')).toBe(false);
12+
expect(checkPasswordRule('abcdef')).toBe(false); // only lowercase
13+
expect(checkPasswordRule('ABCDEF')).toBe(false); // only uppercase
14+
expect(checkPasswordRule('!@#$%^')).toBe(false); // only special chars
15+
16+
// Test two types combination
17+
expect(checkPasswordRule('abc123')).toBe(true); // lowercase + numbers
18+
expect(checkPasswordRule('abcABC')).toBe(true); // lowercase + uppercase
19+
expect(checkPasswordRule('abc!@#')).toBe(true); // lowercase + special chars
20+
expect(checkPasswordRule('ABC!@#')).toBe(true); // uppercase + special chars
21+
expect(checkPasswordRule('ABC123')).toBe(true); // uppercase + numbers
22+
expect(checkPasswordRule('123!@#')).toBe(true); // numbers + special chars
23+
expect(checkPasswordRule('!@123fa')).toBe(true); // numbers + special chars
24+
25+
// Test three types combination
26+
expect(checkPasswordRule('abcABC123')).toBe(true); // lower + upper + numbers
27+
expect(checkPasswordRule('abc123!@#')).toBe(true); // lower + numbers + special
28+
expect(checkPasswordRule('abc!@#123')).toBe(true); // lower + special + numbers
29+
30+
// Test all four types
31+
expect(checkPasswordRule('abcABC123!@#')).toBe(true); // all types
32+
});
33+
});

0 commit comments

Comments
 (0)