82
82
< div id ="login-tab-content " class ="tab-content ">
83
83
< h2 style ="text-align: center; "> Login</ h2 >
84
84
< form >
85
- < input type ="text " id ="name " name ="name " placeholder ="name " required >
86
- < br >
87
85
< input type ="text " id ="email " name ="email " placeholder ="email " required >
88
86
< br >
89
87
< input type ="password " id ="password " name ="password " placeholder ="password " required >
90
88
< br >
91
- < button id ="logInButton " type ="submit " class ="login-button "> Login</ button >
89
+ < button id ="logInButton " type ="submit " class ="login-button " onclick =" logIn() " > Login</ button >
92
90
</ form >
93
91
</ div >
94
92
@@ -109,6 +107,7 @@ <h2 style="text-align: center;">Sign Up</h2>
109
107
110
108
< script >
111
109
const signup_url = 'http://localhost:8085/api/person/post?' ;
110
+ const login_url = 'http://localhost:8085/authenticate' ;
112
111
113
112
function showTab ( tabId ) {
114
113
// Hide all tab contents
@@ -131,7 +130,7 @@ <h2 style="text-align: center;">Sign Up</h2>
131
130
"dob" : encodeURIComponent ( dob )
132
131
} ;
133
132
134
- console . log ( "fetch url: " + signup_url + new URLSearchParams ( params ) ) ;
133
+ console . log ( "sign up fetch url: " + signup_url + new URLSearchParams ( params ) ) ;
135
134
136
135
fetch ( signup_url + new URLSearchParams ( params ) , {
137
136
method : 'POST' ,
@@ -142,23 +141,68 @@ <h2 style="text-align: center;">Sign Up</h2>
142
141
}
143
142
144
143
function logIn ( ) {
145
- let email = document . getElementById ( "new-email" ) . value ;
146
- let password = document . getElementById ( "new-password" ) . value ;
147
- let name = document . getElementById ( "new-name" ) . value ;
144
+ let email = document . getElementById ( "email" ) . value ;
145
+ let password = document . getElementById ( "password" ) . value ;
148
146
149
147
const params = {
150
- email : encodeURIComponent ( email ) ,
151
- password : encodeURIComponent ( password ) ,
152
- name : encodeURIComponent ( name )
148
+ email : email ,
149
+ password : password
153
150
} ;
154
151
155
- fetch ( `${ url } ?${ new URLSearchParams ( params ) } ` , {
156
- method : 'GET' ,
152
+ console . log ( "login fetch url: " + login_url ) ;
153
+ console . log ( "body: " + JSON . stringify ( params ) )
154
+ fetch ( login_url , {
155
+ method : 'POST' ,
156
+ headers : {
157
+ 'Content-Type' : 'application/json' ,
158
+ } ,
159
+ body : JSON . stringify ( params ) ,
157
160
} )
158
161
. then ( response => response . json ( ) )
159
- . then ( data => console . log ( data ) )
162
+ . then ( data => {
163
+ console . log ( data ) ;
164
+ if ( data . status == 200 ) {
165
+ window . location . replace ( "/stats.html" ) ;
166
+ } else {
167
+ console . log ( "bad email and password" ) ;
168
+ }
169
+ } )
160
170
. catch ( error => console . error ( 'Error:' , error ) ) ;
161
171
}
172
+
173
+ function logInXML ( ) {
174
+ let email = document . getElementById ( "email" ) . value ;
175
+ let password = document . getElementById ( "password" ) . value ;
176
+
177
+ const loginData = {
178
+ email : email ,
179
+ password : password
180
+ } ;
181
+
182
+ console . log ( "login fetch url: " + login_url ) ;
183
+
184
+ const xhr = new XMLHttpRequest ( ) ;
185
+ xhr . open ( 'POST' , login_url , true ) ;
186
+ xhr . setRequestHeader ( 'Content-Type' , 'application/json' ) ;
187
+
188
+ xhr . onreadystatechange = function ( ) {
189
+ if ( xhr . readyState === XMLHttpRequest . DONE ) {
190
+ if ( xhr . status === 200 ) {
191
+ const data = JSON . parse ( xhr . responseText ) ;
192
+ console . log ( data ) ;
193
+ if ( data . status == 200 ) {
194
+ window . location . replace ( "/stats.html" ) ;
195
+ } else {
196
+ console . log ( "bad email and password" ) ;
197
+ }
198
+ } else {
199
+ console . error ( 'Error:' , xhr . statusText ) ;
200
+ }
201
+ }
202
+ } ;
203
+
204
+ xhr . send ( JSON . stringify ( loginData ) ) ;
205
+ }
162
206
</ script >
163
207
164
208
0 commit comments