I want to have users log in with a 6 digit code they get from an Authentication app. So far I figured out how to check wether the code is the same in the app and online. It says "454 343" in the Google Authentication app, and by going to https://authenticatorapi.com/validate.aspx?Pin=454343&SecretCode=1234 I will get a response saying either True or False. How exactly do I take that "True" or "False" and set it as a variable in JS?
3 Answers
What you are looking for is sending a HTTP request to that url which tells you if the code is correct.
Previously, you would use XMLHttpRequest but that is becoming somewhat dated.
Instead, I recommend using the fetch API:
(async () => {
var response = await fetch("https://authenticatorapi.com/validate.aspx?Pin=454343&SecretCode=1234");
console.log(response.text()); // should print out "True" or "False"
})();
For more information, check out
Note that the code is inside an async function because fetch() is asynchronous
1 Comment
Raphael Castro
ha I like the clean code! i had to go and fine out what those parenthesis were for and learned what an IFFE was.
this is how I would do it,
var test = new Promise (resolve => {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
resolve(xhttp.responseText)
}
};
xhttp.open("GET", "https://authenticatorapi.com/validate.aspx?Pin=454343&SecretCode=1234", false);
xhttp.send();
})
test.then(result => {
console.log(result)
// expected result is boolean
})