Merge pull request #15 from danieldupriest/LoginScript
Add login script to login page
This commit is contained in:
		
						commit
						f02420e210
					
				
					 2 changed files with 48 additions and 2 deletions
				
			
		|  | @ -11,11 +11,12 @@ | |||
| </head> | ||||
|     <body> | ||||
|         <h1>Login Page</h1> | ||||
|         <form> | ||||
|         <form method="POST"> | ||||
|             username: <input type="text" name="username" required><br> | ||||
|             password: <input type="password" name="password" required><br> | ||||
|             <input type="submit" name="submit" value="submit"> | ||||
|         </form> | ||||
|         <!-- TODO: add script link here --> | ||||
|         <p id="errorReport"><p> | ||||
|         <script src="login.js"></script> | ||||
|     </body> | ||||
| </html> | ||||
|  |  | |||
							
								
								
									
										45
									
								
								front/static/login.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								front/static/login.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,45 @@ | |||
| function displayErrorMessage(errorMessage) { | ||||
|     const errorReport = document.querySelector("#errorReport"); | ||||
|     errorReport.innerHTML = JSON.parse(errorMessage).error; | ||||
| } | ||||
| 
 | ||||
| function postToLoginEndpoint(event) { | ||||
|     event.preventDefault(); | ||||
| 
 | ||||
|     const credentials = { | ||||
|         "username" : this.elements.username.value, | ||||
|         "password" : this.elements.password.value | ||||
|     } | ||||
|     const url = "https://reqres.in/api/login" // mock api service
 | ||||
|     const xhr = new XMLHttpRequest(); | ||||
| 
 | ||||
|     console.log(`User credentials:\n${JSON.stringify(credentials)}`); | ||||
| 
 | ||||
|     xhr.open("POST", url, true); | ||||
|     xhr.setRequestHeader("Content-Type", "application/json"); | ||||
|     xhr.onreadystatechange = function() { | ||||
|         if (this.readyState === 4) { | ||||
|             if (this.status === 200) { | ||||
|                 console.log("LOGIN SUCCESS!"); | ||||
|                 console.log(`Server response:\n${this.response}`); | ||||
|                 token = JSON.parse(this.response).token; | ||||
|                 localStorage.setItem("token", token); | ||||
|                 window.location.replace("dashboard.html"); | ||||
|             } else { | ||||
|                 console.log("LOGIN FAILURE!"); | ||||
|                 console.log(`Server status: ${this.status}`); | ||||
|                 console.log(`Server response:\n${this.response}`); | ||||
|                 displayErrorMessage(this.response); | ||||
|             } | ||||
|         } | ||||
|     }; | ||||
| 
 | ||||
|     xhr.onerror = function() { | ||||
|         alert("Error connecting to the authentication server!"); | ||||
|     }; | ||||
| 
 | ||||
|     xhr.send(JSON.stringify(credentials)); | ||||
| } | ||||
| 
 | ||||
| const form = document.querySelector("form"); | ||||
| form.addEventListener("submit", postToLoginEndpoint); | ||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 hui2018
						hui2018