Skip to content

Instantly share code, notes, and snippets.

@AlexisTM
Created January 20, 2019 13:36
Show Gist options
  • Select an option

  • Save AlexisTM/c7a1f35623a4bde71b6f3670d3e80ceb to your computer and use it in GitHub Desktop.

Select an option

Save AlexisTM/c7a1f35623a4bde71b6f3670d3e80ceb to your computer and use it in GitHub Desktop.
Login on the front-end side using AJAX
// 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