Skip to content

Commit 3568dc8

Browse files
#TRAPWLS-19 Code optimization, UX enhanced and bug fixes
1 parent b82bd0c commit 3568dc8

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

src/loginAuthentication/LoginPage.jsx

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function LoginPage(props) {
2424
//for navigate
2525
const navigateToNextPage = useNavigate();
2626
const {setLoggedInUser} = props;
27-
const [isLoading, setIsLoading] = useState(false);
27+
const [isLoading, setIsLoading] = useState(true);
2828
const [showPopup, setShowPopup] = useState(false);
2929
const [isRegisterClicked, setRegisterClicked] = useState(false);
3030
const isReadInfoOnce = localStorage.getItem('isInfoRead') || false;
@@ -47,26 +47,41 @@ function LoginPage(props) {
4747
const [errorMessagePassword, seterrorMessagePWD] = useState('')
4848
const [errorMessageConfPassword, seterrorMessageConfirmPWD] = useState('')
4949

50+
const handleKeyListeners = (e) => {
51+
if (e.shiftKey === false && e.which === 13) {
52+
e.preventDefault();
53+
// handle Enter key to execute
54+
if (isRegisterClicked) registerClicked();
55+
else loginClicked();
56+
}
57+
58+
}
59+
5060
const handleChange=(e) => {
61+
const inputValueWithoutWhiteSpace = e.target.value.replace(/\s+/, "");
5162
setInput({
5263
...input,
53-
[e.target.name]: e.target.value
64+
[e.target.name]: inputValueWithoutWhiteSpace
5465
})
5566
seterrorMessageEmail('');
5667
seterrorMessagePWD('');
5768
seterrorMessageConfirmPWD('');
5869
}
5970

60-
const handleFocus = (fieldName = "") => {
71+
const handleFocusForPasswordFeedback = (fieldName = "") => {
6172
if (fieldName === "password") {
6273
setPwdFeedbackHint(handlePasswordErrorFeedback());
6374
}
6475
}
6576

66-
const handleBlur = () => {
77+
const handleBlurForPasswordFeedback = () => {
6778
setPwdFeedbackHint(null);
6879
}
6980

81+
const handleInputFieldFocus = (inputId = "") => {
82+
document.getElementById(inputId).focus();
83+
}
84+
7085
//Handle password validation to show error text
7186
const handlePasswordErrorFeedback = useCallback(() => {
7287
if (input.password.length < 1) {
@@ -80,6 +95,7 @@ function LoginPage(props) {
8095
<li style={{ color: /[A-Z]/.test(input.password) ? "green" : "red" }}>At least one uppercase letter</li>
8196
<li style={{ color: /[a-z]/.test(input.password) ? "green" : "red" }}>At least one lowercase letter</li>
8297
<li style={{ color: /\d/.test(input.password) ? "green" : "red" }}>At least one number</li>
98+
<li style={{ color: /[^a-zA-Z0-9\s]/.test(input.password) ? "green" : "red" }}>At least one special character</li>
8399
</ul>
84100
<div>
85101
{passwordValidator(input.password) ?
@@ -99,6 +115,7 @@ function LoginPage(props) {
99115
useEffect(() => {
100116
const timer = setTimeout(() => {
101117
setShowPopup(true);
118+
setIsLoading(false);
102119
}, 1500)
103120

104121
return () => clearTimeout(timer);
@@ -107,7 +124,6 @@ function LoginPage(props) {
107124

108125
// Login via username/password
109126
async function loginClicked(e) {
110-
e.preventDefault();
111127
seterrorMessageEmail('');
112128
seterrorMessagePWD('');
113129

@@ -117,6 +133,7 @@ function LoginPage(props) {
117133

118134
try {
119135
setIsLoading(true);
136+
setPwdFeedbackHint(null);
120137
const signInwithUsername = await handleSignInWithEmailAndPassword(input.username, input.password);
121138
if(signInwithUsername?.success) {
122139
const userObj = {
@@ -146,20 +163,29 @@ function LoginPage(props) {
146163
if (!isValidated) return;
147164

148165
// validate password & confirm password
149-
if (input.confirmPassword !== input.password) {
166+
if (input.confirmPassword.length > 0 && (input.confirmPassword !== input.password)) {
167+
handleInputFieldFocus("confirmPassword");
150168
return seterrorMessageConfirmPWD("Confirm password should match with your password")
169+
} else if (input.confirmPassword !== input.password) {
170+
handleInputFieldFocus("confirmPassword");
171+
return seterrorMessageConfirmPWD("Enter your password here to confirm*")
151172
}
152173

153174
try {
154175
setIsLoading(true);
176+
setPwdFeedbackHint(null);
155177
const registerWithUsername = await handleCreateUserWithEmailAndPassword(input.username, input.password);
156178
if (registerWithUsername?.success) {
157179
notifyToast({
158180
textContent: "Registered successful!! Use your credentials to Login here!!",
159181
type: "info"
160182
})
161183
} else {
162-
notifyToast({textContent: registerWithUsername?.data?.code, type: "error"})
184+
const emailErrorText = "Email already registered, use email to SignIn/Login here.";
185+
notifyToast({
186+
textContent: (registerWithUsername?.data?.code === "auth/email-already-in-use") ? emailErrorText : registerWithUsername?.data?.code,
187+
type: "error"
188+
})
163189
}
164190
navigateToRegister_Login();
165191
} catch(e) {
@@ -172,12 +198,14 @@ function LoginPage(props) {
172198
//handle validation for username/email and password based on input
173199
function handleValidationForUserInput(username, password) {
174200
if (!emailValidator(username)) {
175-
seterrorMessageEmail("Please Enter valid username/email format*");
201+
seterrorMessageEmail("Enter valid username/email format*");
202+
handleInputFieldFocus("username");
176203
return false;
177204
}
178205

179206
if (!passwordValidator(password)) {
180-
seterrorMessagePWD("Please Enter your password*");
207+
seterrorMessagePWD(!isRegisterClicked ? "Enter your valid password*" : "Enter your password here");
208+
handleInputFieldFocus("password");
181209
return false;
182210
}
183211
return true;
@@ -188,6 +216,12 @@ function LoginPage(props) {
188216
inputUsernameRef.current.value = "";
189217
inputPasswordRef.current.value = "";
190218
setRegisterClicked(!isRegisterClicked);
219+
setInput({
220+
username: "",
221+
password: "",
222+
confirmPassword: ""
223+
})
224+
handleInputFieldFocus("username");
191225
seterrorMessageEmail("");
192226
seterrorMessagePWD("");
193227
seterrorMessageConfirmPWD("");
@@ -231,53 +265,60 @@ function LoginPage(props) {
231265
if(isRead) {
232266
setShowPopup(false);
233267
localStorage.setItem('isInfoRead', true);
268+
handleInputFieldFocus("username");
234269
}
235270
}
236271

237272
return(
238273

239-
<div className="cover">
274+
<div className="cover" onKeyDown={(e) => {handleKeyListeners(e)}}>
240275
{/* Popup shown initially */}
241-
{showPopup && !isReadInfoOnce && (<Popup setClosePopup={closePopupInfo}/>)}
276+
{showPopup && !isReadInfoOnce && (<Popup setClosePopup={closePopupInfo} stopLoader={setIsLoading}/>)}
242277

243278
<u><h2>{isRegisterClicked ? "Register Here " : "Login Here "}</h2></u>
244279
<p>{!isRegisterClicked ? "Sign in" : "Register here"} & let's get started</p>
245280
{errorMessageEmail.length > 0 && (<div className="error1">{errorMessageEmail}</div>)}
246281
<input
282+
id="username"
247283
type="text"
248284
className="input"
249285
placeholder="Enter your username/email"
250286
name="username"
251287
onChange={handleChange}
288+
value={input.username}
252289
ref={inputUsernameRef}
253-
onFocus={() => handleFocus("username")}
254-
onBlur={handleBlur}
290+
onFocus={() => handleFocusForPasswordFeedback("username")}
291+
onBlur={handleBlurForPasswordFeedback}
255292
/>
256293

257294
{errorMessagePassword.length > 0 && (<div className="error2">{errorMessagePassword}</div>)}
258295
<input
296+
id="password"
259297
type="text"
260298
className="input"
261299
placeholder="Enter your password"
262300
name="password"
263301
onChange={handleChange}
302+
value={input.password}
264303
ref={inputPasswordRef}
265-
onFocus={() => handleFocus("password")}
266-
onBlur={handleBlur}
304+
onFocus={() => handleFocusForPasswordFeedback("password")}
305+
onBlur={handleBlurForPasswordFeedback}
267306
/>
268307

269308
{errorMessageConfPassword.length > 0 && (<div className="error3" style={{marginLeft:"-130px", marginBottom:"-20px", color:"red"}}>
270309
{errorMessageConfPassword}</div>
271310
)}
272311
{isRegisterClicked &&
273312
<input
313+
id="confirmPassword"
274314
type="text"
275315
className="input"
276316
placeholder="Confirm password"
277317
name="confirmPassword"
278318
onChange={handleChange}
279-
onFocus={() => handleFocus("confirmPassword")}
280-
onBlur={handleBlur}
319+
value={input.confirmPassword}
320+
onFocus={() => handleFocusForPasswordFeedback("confirmPassword")}
321+
onBlur={handleBlurForPasswordFeedback}
281322
/>
282323
}
283324

src/utility/regexValidator.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export const emailValidator = username => {
44
}
55

66
export const passwordValidator = password => {
7-
const passwordRegex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/;
7+
const passwordRegex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$+%^&*_\-~]).{8,}$/;
88
return passwordRegex.test(password);
99
}

0 commit comments

Comments
 (0)