Skip to content

Commit cdc1ce0

Browse files
regex check on login form
1 parent 140ff02 commit cdc1ce0

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/components/pages/auth/login/index.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ import { saveUserToStore } from "@/redux/reducers/authReducer";
1212
import { toastify } from "@/helper/toastify";
1313

1414
// API
15-
import { login, loginWithGithub, logoutUser } from "@/backend/auth.api";
15+
import { login, loginWithGithub } from "@/backend/auth.api";
1616

1717
// Icons
1818
import { userCollectionDB } from "@/types/auth";
1919
import { setCookie } from "nookies";
2020

21+
const disallowedPasswordRegex: RegExp = /[^A-Za-z\d@_!#$%^&]/;
22+
const passwordRegex: RegExp = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[@_!#$%^&])[A-Za-z\d@_!#$%^&]{6,24}$/;
23+
24+
2125
export default function LoginComponent() {
2226
const [isLoading, setIsLoading] = useState(false);
2327
const [showPassword, setShowPassword] = useState(false);
@@ -44,14 +48,22 @@ export default function LoginComponent() {
4448
try {
4549
setIsLoading(true);
4650

47-
if (data.password.length < 6 || data.password.length > 16) {
48-
throw new Error("Password should be in range of 6 to 16 characters");
49-
}
50-
5151
if (data.email === "" || data.password === "") {
5252
throw new Error("Email and Password fields should be filled");
5353
}
5454

55+
if (data.password.length < 6 || data.password.length > 24) {
56+
throw new Error("Password should be in range of 6 to 24 characters");
57+
}
58+
59+
if (disallowedPasswordRegex.test(data.password)) {
60+
throw new Error("Invalid password. Only the following special characters are allowed: @, _, !, #, $, %, ^, &");
61+
}
62+
63+
if (!passwordRegex.test(data.password)) {
64+
throw new Error("password format not matched");
65+
}
66+
5567
const resp = await login(data.email, data.password);
5668

5769
if (resp && resp.email === data.email) {

0 commit comments

Comments
 (0)