@@ -10,23 +10,11 @@ document.addEventListener("DOMContentLoaded", () => {
1010 )
1111 ) ;
1212
13- const usernameTab = document . getElementById ( "username-tab" ) ;
14- const passwordTab = document . getElementById ( "password-tab" ) ;
1513 const usernameInput = document . getElementById ( "username" ) ;
1614 const passwordInput = document . getElementById ( "password" ) ;
1715 const loginForm = document . getElementById ( "kc-form-login" ) ;
1816 const passwordToggleButton = document . getElementById ( "password-toggle" ) ;
1917
20- const Tabs = {
21- username : "username" ,
22- password : "password" ,
23- } ;
24-
25- let username = "" ;
26- let password = "" ;
27- let isUsernameInvalid = false ;
28- let isPasswordInvalid = false ;
29- let currentTab = Tabs . username ;
3018 let showPassword = false ;
3119
3220 passwordToggleButton . addEventListener ( "click" , ( ) => {
@@ -42,69 +30,17 @@ document.addEventListener("DOMContentLoaded", () => {
4230 }
4331 } ) ;
4432
45- /** Updates the login UI to reflect the internal state. */
46- function syncUiWithState ( ) {
47- usernameInput . value = username ;
48- passwordInput . value = password ;
49- usernameInput . classList . toggle ( "invalid" , isUsernameInvalid ) ;
50- passwordInput . classList . toggle ( "invalid" , isPasswordInvalid ) ;
51- usernameTab . classList . toggle ( "hidden" , currentTab !== Tabs . username ) ;
52- passwordTab . classList . toggle ( "hidden" , currentTab !== Tabs . password ) ;
53- }
54-
55- /**
56- * Called when the form is submitted.
57- * This returns whether the form submission should be prevented
58- * or not (i.e. whether the form data should be sent to the server).
59- *
60- * The latter should only happen when both the username and password are
61- * valid for submission (i.e. non-empty).
62- */
63- function handleFormSubmit ( ) {
64- if ( currentTab === Tabs . username ) {
65- username = usernameInput . value ;
66- isUsernameInvalid = ! username ;
67-
68- if ( ! isUsernameInvalid ) {
69- currentTab = Tabs . password ;
70- }
71-
72- // In the username step, we always want to prevent the form
73- // from being submitted because the user still has to enter
74- // the password.
75- syncUiWithState ( ) ;
76- passwordInput . focus ( ) ;
77- return true ;
78- } else if ( currentTab === Tabs . password ) {
79- password = passwordInput . value ;
80- isPasswordInvalid = ! password ;
81-
82- // In the password step we only want to prevent the form
83- // from being submitted while the password is invalid.
84- syncUiWithState ( ) ;
85- return isPasswordInvalid ;
86- } else {
87- console . error (
88- `Arrived at an invalid tab: "${ currentTab } ". ` +
89- `This is most likely an error in the code and should be fixed. ` +
90- `Resetting to the initial tab.`
91- ) ;
92-
93- currentTab = Tabs . username ;
94- syncUiWithState ( ) ;
95- return true ;
96- }
97- }
98-
9933 loginForm . onsubmit = ( e ) => {
100- const shouldPreventFormSubmit = handleFormSubmit ( ) ;
101- if ( shouldPreventFormSubmit ) {
34+ const username = usernameInput . value . trim ( ) ;
35+ const password = passwordInput . value . trim ( ) ;
36+
37+ if ( ! username || ! password ) {
10238 e . preventDefault ( ) ;
10339 return false ;
10440 } else {
10541 // This has been moved out of the KC ftl template.
10642 // It is, by default, present in the following form:
107- // <form onsubmit="login.disabled = true; return true;" action="${url.loginAction}" method="post" ...>
43+ // <form onsubmit="login.disabled = true; return true;" action="${url.loginAction}" method="post" ...>
10844 // It is moved here, because we require special submission
10945 // handling due to the form having 2 steps.
11046 login . disabled = true ;
0 commit comments