Created
January 20, 2019 13:36
-
-
Save AlexisTM/c7a1f35623a4bde71b6f3670d3e80ceb to your computer and use it in GitHub Desktop.
Login on the front-end side using AJAX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // To be used to set a cookie | |
| function setCookie(cname, cvalue, exdays) { | |
| var d = new Date(); | |
| d.setTime(d.getTime() + (exdays * 86400000)); | |
| var expires = "expires="+d.toUTCString(); | |
| document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; | |
| } | |
| // Login through an AJAX call | |
| function login(username, password) { | |
| let xhttp = new XMLHttpRequest(); | |
| xhttp.onreadystatechange = function() { | |
| if (this.readyState == 4 && this.status == 200) { | |
| setCookie("token", this.responseText, 100); | |
| setCookie("username", username, 100); | |
| console.log('Success'); | |
| } else if(this.readyState == 4 && this.status == 403) { | |
| console.error('Failure'); | |
| } | |
| }; | |
| xhttp.open("GET", "login?username=" + username + "&password=" + password, true); | |
| xhttp.send(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment