2017-09-20 23:39:20 +02:00
|
|
|
function sendAuthRequest (channelName, password, url) { // url === /signup or /login
|
2017-09-20 18:49:05 +02:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
// make sure the claim name is still available
|
|
|
|
let xhttp;
|
|
|
|
const params = `username=${channelName}&password=${password}`;
|
2017-09-20 23:39:20 +02:00
|
|
|
console.log(params, url);
|
2017-09-20 18:49:05 +02:00
|
|
|
xhttp = new XMLHttpRequest();
|
2017-09-20 23:39:20 +02:00
|
|
|
xhttp.open('POST', url, true);
|
2017-09-20 18:49:05 +02:00
|
|
|
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
|
|
|
xhttp.responseType = 'json';
|
|
|
|
xhttp.onreadystatechange = function() {
|
|
|
|
if (this.readyState == 4 ) {
|
|
|
|
if ( this.status == 200) {
|
|
|
|
if (this.response == true) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
2017-09-21 00:43:42 +02:00
|
|
|
reject(new NameError('Your request succedded but could not be completed'));
|
2017-09-20 18:49:05 +02:00
|
|
|
}
|
2017-09-21 00:43:42 +02:00
|
|
|
} else if (this.status == 401) {
|
|
|
|
reject('Incorrect username or password')
|
2017-09-20 18:49:05 +02:00
|
|
|
} else {
|
2017-09-21 00:43:42 +02:00
|
|
|
reject('Auth request failed with status:' + this.status);
|
2017-09-20 18:49:05 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhttp.send(params);
|
|
|
|
});
|
|
|
|
}
|