2

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?

1
  • FYI the server is denying all requests, and there is no documentation for this API; I would find a different one Commented Jan 19, 2020 at 21:52

3 Answers 3

2

Try this:

(async () => {
    const
        url = 'https://authenticatorapi.com/validate.aspx?Pin=454343&SecretCode=1234',
        response = await fetch(url),
        answer = await response.text() === 'True';

    console.log(answer);
})();
Sign up to request clarification or add additional context in comments.

Comments

2

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

ha I like the clean code! i had to go and fine out what those parenthesis were for and learned what an IFFE was.
1

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
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.