[英](HTML/Js) Authentication Using Prompts and Promises
我在下面有一些代碼:
async function authenticateUser() {
let authenticated = false;
socketTx = io();
socketRx = io.connect();
var user = prompt("Please enter username", "");
var pass = prompt("Please enter your password", "");
socketTx.emit('authChannel', {username: user, password: pass});
let promise = new Promise((resolve, reject) => {
socketRx.on('authChannel', function(auth) {
//console.log(auth);
resolve(auth);
});
});
authenticated = await promise;
console.log(authenticated)
if (authenticated == false) {
user;
pass;
} else {authenticated == true}
}
socketTx
將user
/ pass
數據發送到服務器以在Node.js
進行 PAM 身份驗證。 socketRx
接收用戶是否通過身份驗證的true
/ false
。
期望的行為是提示向用戶發送垃圾郵件,直到提供正確的“登錄”信息。 我以為我在驗證身份驗證為假后再次調用變量user
/ pass
時捕獲了這種行為,但提示不會顯示第二次(或更多)次。 我可以確認我在console.log(authenticated)
收到了正確的信息。
有什么明顯的我遺漏了嗎?
提前致謝。
根據評論:
async function authenticateUser() {
let authenticated = false;
socketTx = io();
socketRx = io.connect();
var user = prompt("Please enter username", "");
var pass = prompt("Please enter your password", "");
socketTx.emit('authChannel', {username: user, password: pass});
let promise = new Promise((resolve, reject) => {
socketRx.on('authChannel', function(auth) {
//console.log(auth);
resolve(auth);
});
});
authenticated = await promise;
console.log(authenticated)
if (authenticated == false) {
authenticateUser(); // recall method
} else {authenticated == true}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.