使用Javascript验证身份验证代码

使用Javascript验证身份验证代码,javascript,Javascript,我想让用户使用从身份验证应用程序获得的6位数代码登录。 到目前为止,我找到了如何检查应用程序和在线程序中的代码是否相同的方法。 它在谷歌认证应用程序中显示“454343”,并且 我会得到一个回答,要么是真的,要么是假的。 我如何准确地将“真”或“假”设置为JS中的变量?这就是我要做的 var test = new Promise (resolve => { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange

我想让用户使用从身份验证应用程序获得的6位数代码登录。 到目前为止,我找到了如何检查应用程序和在线程序中的代码是否相同的方法。 它在谷歌认证应用程序中显示“454343”,并且 我会得到一个回答,要么是真的,要么是假的。
我如何准确地将“真”或“假”设置为JS中的变量?

这就是我要做的

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
})
试试这个:

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

    console.log(answer);
})();

您要查找的是向该url发送HTTP请求,该请求会告诉您代码是否正确

以前,您可能会使用
XMLHttpRequest
,但这已经有点过时了

相反,我建议使用
fetch
API:

(异步()=>{
var响应=等待获取(“https://authenticatorapi.com/validate.aspx?Pin=454343&SecretCode=1234");
console.log(response.text());//应打印“True”或“False”
})();
有关更多信息,请查看


请注意,代码位于
异步函数中
,因为
fetch()
是异步的

FYI服务器拒绝所有请求,并且没有此API的文档;我会找到一个不同的我喜欢干净的代码!我必须去弄清楚那些括号是用来干什么的,然后才知道什么是IFFE。