Skip to content

Commit 002b0d5

Browse files
Merge pull request #2027 from OpenSignLabs/build_deps
fix: handle password validation in change password page
2 parents 47348b9 + ac86b6e commit 002b0d5

File tree

7 files changed

+148
-54
lines changed

7 files changed

+148
-54
lines changed

apps/OpenSign/public/locales/de/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"login-page": "Anmeldeseite",
7272
"or": "ODER",
7373
"signup-page": "Registrierungsseite",
74+
"password-match-length": "Neues Passwort und Bestätigungspasswort sollten übereinstimmen",
7475
"password-length": "Das Passwort sollte 8 Zeichen lang sein",
7576
"password-case": "Das Passwort sollte einen Großbuchstaben, einen Kleinbuchstaben und eine Zahl enthalten",
7677
"password-special-char": "Das Passwort sollte ein Sonderzeichen enthalten",

apps/OpenSign/public/locales/en/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"login-page": "Login P+age",
7272
"or": "OR",
7373
"signup-page": "Signup page",
74+
"password-match-length": "New password and confirmation password should match",
7475
"password-length": "Password should be 8 characters long",
7576
"password-case": "Password should contain uppercase letter, lowercase letter, digit",
7677
"password-special-char": " Password should contain special character",

apps/OpenSign/public/locales/es/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"login-page": "Página de inicio de sesión",
7272
"or": "O",
7373
"signup-page": "Página de registro",
74+
"password-match-length": "La nueva contraseña y la confirmación deben coincidir",
7475
"password-length": "La contraseña debe tener 8 caracteres",
7576
"password-case": "La contraseña debe incluir mayúsculas, minúsculas y números",
7677
"password-special-char": " La contraseña debe incluir caracteres especiales",

apps/OpenSign/public/locales/fr/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"login-page": "Page de connexion",
7272
"or": "OU",
7373
"signup-page": "Page d'inscription",
74+
"password-match-length": "Le nouveau mot de passe et la confirmation doivent correspondre",
7475
"password-length": "Le mot de passe doit être de plus de 8 caractères",
7576
"password-case": "Le mot de passe doit contenir une lettre majuscule, minuscule et un chiffre ",
7677
"password-special-char": "Le mot de passe doit contenir un caractère spécial",

apps/OpenSign/public/locales/hi/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"login-page": "लॉगिन पृष्ठ",
7272
"or": "या",
7373
"signup-page": "साइनअप पृष्ठ",
74+
"password-match-length": "नया पासवर्ड और पुष्टि पासवर्ड मेल खाना चाहिए",
7475
"password-length": "पासवर्ड 8 वर्ण लंबा होना चाहिए",
7576
"password-case": "पासवर्ड में अपरकेस अक्षर, लोअरकेस अक्षर, अंक होना चाहिए",
7677
"password-special-char": " पासवर्ड में विशेष वर्ण होना चाहिए",

apps/OpenSign/public/locales/it/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"login-page": "Pagina di accesso",
7272
"or": "O",
7373
"signup-page": "Pagina di registrazione",
74+
"password-match-length": "La nuova password e la conferma devono corrispondere",
7475
"password-length": "La password deve contenere almeno 8 caratteri",
7576
"password-case": "La password deve contenere una lettera maiuscola, una minuscola e un numero",
7677
"password-special-char": "La password deve contenere un carattere speciale",

apps/OpenSign/src/pages/ChangePassword.jsx

Lines changed: 142 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,70 @@ function ChangePassword() {
88
const [currentpassword, setCurrentPassword] = useState("");
99
const [newpassword, setnewpassword] = useState("");
1010
const [confirmpassword, setconfirmpassword] = useState("");
11+
const [lengthValid, setLengthValid] = useState(false);
12+
const [caseDigitValid, setCaseDigitValid] = useState(false);
13+
const [specialCharValid, setSpecialCharValid] = useState(false);
14+
const [showNewPassword, setNewShowPassword] = useState(false);
15+
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
16+
const toggleNewPasswordVisibility = () => {
17+
setNewShowPassword(!showNewPassword);
18+
};
19+
const toggleConfirmPasswordVisibility = () => {
20+
setShowConfirmPassword(!showConfirmPassword);
21+
};
22+
23+
const handlePasswordChange = (e) => {
24+
const newPassword = e.target.value;
25+
setconfirmpassword(newPassword);
26+
// Check conditions separately
27+
setLengthValid(newPassword.length >= 8);
28+
setCaseDigitValid(
29+
/[a-z]/.test(newPassword) &&
30+
/[A-Z]/.test(newPassword) &&
31+
/\d/.test(newPassword)
32+
);
33+
setSpecialCharValid(/[!@#$%^&*()\-_=+{};:,<.>]/.test(newPassword));
34+
};
1135
const handleSubmit = async (evt) => {
1236
evt.preventDefault();
1337
try {
1438
if (newpassword === confirmpassword) {
15-
Parse.User.logIn(localStorage.getItem("userEmail"), currentpassword)
16-
.then(async (user) => {
17-
if (user) {
18-
const User = new Parse.User();
19-
const query = new Parse.Query(User);
20-
await query.get(user.id).then((user) => {
21-
// Updates the data we want
22-
user.set("password", newpassword);
23-
user
24-
.save()
25-
.then(async () => {
26-
let _user = user.toJSON();
27-
if (_user) {
28-
await Parse.User.become(_user.sessionToken);
29-
localStorage.setItem("accesstoken", _user.sessionToken);
30-
}
31-
alert(t("password-update-alert-1"));
32-
})
33-
.catch((error) => {
34-
console.log("err", error);
35-
alert(t("something-went-wrong-mssg"));
36-
});
37-
});
38-
} else {
39-
alert(t("password-update-alert-2"));
40-
}
41-
})
42-
.catch((error) => {
43-
alert(t("password-update-alert-3"));
44-
console.error("Error while logging in user", error);
45-
});
39+
if (lengthValid && caseDigitValid && specialCharValid) {
40+
Parse.User.logIn(localStorage.getItem("userEmail"), currentpassword)
41+
.then(async (user) => {
42+
if (user) {
43+
const User = new Parse.User();
44+
const query = new Parse.Query(User);
45+
await query.get(user.id).then((user) => {
46+
// Updates the data we want
47+
user.set("password", newpassword);
48+
user
49+
.save()
50+
.then(async () => {
51+
let _user = user.toJSON();
52+
if (_user) {
53+
await Parse.User.become(_user.sessionToken);
54+
localStorage.setItem("accesstoken", _user.sessionToken);
55+
}
56+
setCurrentPassword("");
57+
setnewpassword("");
58+
setconfirmpassword("");
59+
alert(t("password-update-alert-1"));
60+
})
61+
.catch((error) => {
62+
console.log("err", error);
63+
alert(t("something-went-wrong-mssg"));
64+
});
65+
});
66+
} else {
67+
alert(t("password-update-alert-2"));
68+
}
69+
})
70+
.catch((error) => {
71+
alert(t("password-update-alert-3"));
72+
console.error("Error while logging in user", error);
73+
});
74+
}
4675
} else {
4776
alert(t("password-update-alert-4"));
4877
}
@@ -80,34 +109,93 @@ function ChangePassword() {
80109
<label htmlFor="newpassword" className="text-xs block ml-1">
81110
{t("new-password")}
82111
</label>
83-
<input
84-
type="password"
85-
name="newpassword"
86-
value={newpassword}
87-
onChange={(e) => setnewpassword(e.target.value)}
88-
className="op-input op-input-bordered op-input-sm text-xs w-full"
89-
placeholder={t("new-password")}
90-
onInvalid={(e) => e.target.setCustomValidity(t("input-required"))}
91-
onInput={(e) => e.target.setCustomValidity("")}
92-
required
93-
/>
112+
<div className="relative">
113+
<input
114+
type={showNewPassword ? "text" : "password"}
115+
name="newpassword"
116+
value={newpassword}
117+
onChange={(e) => setnewpassword(e.target.value)}
118+
className="op-input op-input-bordered op-input-sm text-xs w-full"
119+
placeholder={t("new-password")}
120+
onInvalid={(e) =>
121+
e.target.setCustomValidity(t("input-required"))
122+
}
123+
onInput={(e) => e.target.setCustomValidity("")}
124+
required
125+
/>
126+
<span
127+
className={`absolute top-[50%] right-[10px] -translate-y-[50%] cursor-pointer text-base-content`}
128+
onClick={toggleNewPasswordVisibility}
129+
>
130+
{showNewPassword ? (
131+
<i className="fa fa-eye-slash" /> // Close eye icon
132+
) : (
133+
<i className="fa fa-eye" /> // Open eye icon
134+
)}
135+
</span>
136+
</div>
94137
</div>
95138
<div>
96-
<label htmlFor="newpassword" className="text-xs block ml-1">
139+
<label htmlFor="confirmpassword" className="text-xs block ml-1">
97140
{t("confirm-password")}
98141
</label>
99-
<input
100-
type="password"
101-
name="confirmpassword"
102-
className="op-input op-input-bordered op-input-sm text-xs w-full"
103-
value={confirmpassword}
104-
onChange={(e) => setconfirmpassword(e.target.value)}
105-
placeholder={t("confirm-password")}
106-
onInvalid={(e) => e.target.setCustomValidity(t("input-required"))}
107-
onInput={(e) => e.target.setCustomValidity("")}
108-
required
109-
/>
142+
<div className="relative">
143+
<input
144+
type={showConfirmPassword ? "text" : "password"}
145+
name="confirmpassword"
146+
className="op-input op-input-bordered op-input-sm text-xs w-full"
147+
value={confirmpassword}
148+
onChange={handlePasswordChange}
149+
placeholder={t("confirm-password")}
150+
onInvalid={(e) =>
151+
e.target.setCustomValidity(t("input-required"))
152+
}
153+
onInput={(e) => e.target.setCustomValidity("")}
154+
required
155+
/>
156+
<span
157+
className={`absolute top-[50%] right-[10px] -translate-y-[50%] cursor-pointer text-base-content`}
158+
onClick={toggleConfirmPasswordVisibility}
159+
>
160+
{showConfirmPassword ? (
161+
<i className="fa fa-eye-slash" /> // Close eye icon
162+
) : (
163+
<i className="fa fa-eye" /> // Open eye icon
164+
)}
165+
</span>
166+
</div>
110167
</div>
168+
{confirmpassword.length > 0 && (
169+
<div className="mt-1 text-[11px]">
170+
{newpassword.length > 0 && (
171+
<p
172+
className={`${newpassword === confirmpassword ? "text-green-600" : "text-red-600"} text-[11px] mt-1`}
173+
>
174+
{newpassword === confirmpassword ? "✓" : "✗"}{" "}
175+
{t("password-match-length")}
176+
</p>
177+
)}
178+
<p
179+
className={`${lengthValid ? "text-green-600" : "text-red-600"}`}
180+
>
181+
{lengthValid ? "✓" : "✗"} {t("password-length")}
182+
</p>
183+
<p
184+
className={`${
185+
caseDigitValid ? "text-green-600" : "text-red-600"
186+
}`}
187+
>
188+
{caseDigitValid ? "✓" : "✗"} {t("password-case")}
189+
</p>
190+
<p
191+
className={`${
192+
specialCharValid ? "text-green-600" : "text-red-600"
193+
}`}
194+
>
195+
{specialCharValid ? "✓" : "✗"} {t("password-special-char")}
196+
</p>
197+
</div>
198+
)}
111199
<button
112200
type="submit"
113201
className="op-btn op-btn-primary shadow-md mt-2"

0 commit comments

Comments
 (0)