issue(31):Password validation fixed
This commit is contained in:
parent
5fbd17faed
commit
7e19e51708
65
src/App.tsx
65
src/App.tsx
|
|
@ -6,7 +6,7 @@ import { getCountryRuleByName } from './constants/countries';
|
|||
import type { ModuleItem, Currency, UserProfile, VerificationResult, PublicPlan } from './types';
|
||||
import { API_BASE } from './services/api';
|
||||
import { loadRazorpayScript } from './services/razorpay';
|
||||
import { cleanPhoneInput, validatePhoneNumber } from './utils/validation';
|
||||
import { cleanPhoneInput, validatePhoneNumber, validatePassword } from './utils/validation';
|
||||
|
||||
// Modular Subcomponents
|
||||
import { StoreHeader } from './components/layout/StoreHeader';
|
||||
|
|
@ -430,32 +430,12 @@ export default function App() {
|
|||
const endpoint = authMode === 'login' ? '/api/auth/login' : '/api/auth/register';
|
||||
|
||||
if (authMode === 'register') {
|
||||
if (authPassword.length < 8) {
|
||||
setAuthError('Password must be at least 8 characters long.');
|
||||
const pwdError = validatePassword(authPassword, 'Password');
|
||||
if (pwdError) {
|
||||
setAuthError(pwdError);
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
if (!/[A-Z]/.test(authPassword)) {
|
||||
setAuthError('Password must contain at least one uppercase letter.');
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
if (!/[0-9]/.test(authPassword)) {
|
||||
setAuthError('Password must contain at least one number.');
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
if (/\p{Extended_Pictographic}/u.test(authPassword)) {
|
||||
setAuthError('Password must not contain emojis.');
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
if (!/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~]/.test(authPassword)) {
|
||||
setAuthError('Password must contain at least one special character.');
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const fullPhone = authPhone ? `${authDialCode}${authPhone}` : undefined;
|
||||
|
|
@ -499,20 +479,9 @@ export default function App() {
|
|||
setChangePasswordError('Please enter your current password.');
|
||||
return;
|
||||
}
|
||||
if (changeNewPassword.length < 8) {
|
||||
setChangePasswordError('New password must be at least 8 characters long.');
|
||||
return;
|
||||
}
|
||||
if (!/[A-Z]/.test(changeNewPassword)) {
|
||||
setChangePasswordError('New password must contain at least one uppercase letter.');
|
||||
return;
|
||||
}
|
||||
if (!/[0-9]/.test(changeNewPassword)) {
|
||||
setChangePasswordError('New password must contain at least one number.');
|
||||
return;
|
||||
}
|
||||
if (!/[^A-Za-z0-9\s]/.test(changeNewPassword)) {
|
||||
setChangePasswordError('New password must contain at least one special character.');
|
||||
const pwdError = validatePassword(changeNewPassword, 'New password');
|
||||
if (pwdError) {
|
||||
setChangePasswordError(pwdError);
|
||||
return;
|
||||
}
|
||||
if (changeNewPassword !== changeConfirmPassword) {
|
||||
|
|
@ -581,23 +550,9 @@ export default function App() {
|
|||
setAuthLoading(true);
|
||||
setAuthError(null);
|
||||
|
||||
if (resetNewPassword.length < 8) {
|
||||
setAuthError('New password must be at least 8 characters long.');
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
if (!/[A-Z]/.test(resetNewPassword)) {
|
||||
setAuthError('New password must contain at least one uppercase letter.');
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
if (!/[0-9]/.test(resetNewPassword)) {
|
||||
setAuthError('New password must contain at least one number.');
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
if (!/[^A-Za-z0-9\s]/.test(resetNewPassword)) {
|
||||
setAuthError('New password must contain at least one special character.');
|
||||
const pwdError = validatePassword(resetNewPassword, 'New password');
|
||||
if (pwdError) {
|
||||
setAuthError(pwdError);
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import {
|
|||
} from 'lucide-react';
|
||||
import type { UserProfile, ModuleItem, Currency } from '../../types';
|
||||
import { COUNTRY_CODES, getCountryRuleByName } from '../../constants/countries';
|
||||
import { cleanPhoneInput } from '../../utils/validation';
|
||||
import { cleanPhoneInput, SPECIAL_CHAR_REGEX } from '../../utils/validation';
|
||||
|
||||
interface ProfileDrawerProps {
|
||||
isOpen: boolean;
|
||||
|
|
@ -289,9 +289,12 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
|||
<span className={/[0-9]/.test(changeNewPassword) ? 'text-emerald-600 font-bold' : ''}>
|
||||
• 1 number
|
||||
</span>
|
||||
<span className={/[^A-Za-z0-9\s]/.test(changeNewPassword) ? 'text-emerald-600 font-bold' : ''}>
|
||||
<span className={SPECIAL_CHAR_REGEX.test(changeNewPassword) ? 'text-emerald-600 font-bold' : ''}>
|
||||
• 1 special char
|
||||
</span>
|
||||
<span className={changeNewPassword.length > 0 && !/\s/.test(changeNewPassword) ? 'text-emerald-600 font-bold' : ''}>
|
||||
• No spaces
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<button
|
||||
|
|
|
|||
|
|
@ -74,3 +74,40 @@ export const isValidPhoneNumber = (
|
|||
): boolean => {
|
||||
return validatePhoneNumber(val, dialCode, country) === null;
|
||||
};
|
||||
|
||||
export const SPECIAL_CHAR_REGEX = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~]/;
|
||||
|
||||
/**
|
||||
* Validates password strength consistently across registration, profile change, and password reset:
|
||||
* - Minimum 8 characters
|
||||
* - No spaces (whitespace strictly disallowed)
|
||||
* - At least one uppercase letter
|
||||
* - At least one number
|
||||
* - No emojis
|
||||
* - At least one special character from standard symbol set
|
||||
*/
|
||||
export const validatePassword = (password: string, fieldName = 'Password'): string | null => {
|
||||
if (!password) {
|
||||
return `${fieldName} is required.`;
|
||||
}
|
||||
if (password.length < 8) {
|
||||
return `${fieldName} must be at least 8 characters long.`;
|
||||
}
|
||||
if (/\s/.test(password)) {
|
||||
return `${fieldName} must not contain spaces.`;
|
||||
}
|
||||
if (!/[A-Z]/.test(password)) {
|
||||
return `${fieldName} must contain at least one uppercase letter.`;
|
||||
}
|
||||
if (!/[0-9]/.test(password)) {
|
||||
return `${fieldName} must contain at least one number.`;
|
||||
}
|
||||
if (/\p{Extended_Pictographic}/u.test(password)) {
|
||||
return `${fieldName} must not contain emojis.`;
|
||||
}
|
||||
if (!SPECIAL_CHAR_REGEX.test(password)) {
|
||||
return `${fieldName} must contain at least one special character.`;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue