我有一个简单的电子应用程序,它包装在一个提示输入用户名和密码的网络应用程序中。
问题:
1)当用户输入错误的凭据时,再次出现authWindow。使用正确的凭据,第二次登录不会发生。
2)当用户两次输入错误的凭据时,不再出现authWindow。
感谢您的帮助。
这是我的代码:
const { app, BrowserWindow, ipcMain } = require('electron');
app.on("login", (event, webContents, request, authInfo, callback) => {
event.preventDefault();
createAuthWindow().then(credentials => {
callback(credentials.username, credentials.password);
});
});
function createAuthWindow() {
authWindow = new BrowserWindow({
show: false,
width: 400,
height: 200,
webPreferences: {
nodeIntegration: true
},
title: "Authentication",
});
authWindow.on('ready-to-show', function (){
authWindow.show();
});
authWindow.loadFile('password-form.html');
return new Promise((resolve, reject) => {
ipcMain.once('password-form-submission', (event, username, password) => {
authWindow.close();
const credentials = {
username,
password
};
resolve(credentials);
});
});
}
function createChatWindow() {
chatWindow = new BrowserWindow({
show: false,
width: 1000,
height: 800,
webPreferences: {
devTools: true
},
icon: __dirname + '/build/icon.png',
title: "Messenger",
});
chatWindow.once('ready-to-show', function (){
chatWindow.show();
});
chatWindow.loadURL('https://example.com');
chatWindow.webContents.openDevTools();
}
app.on('ready', createChatWindow);
答案 0 :(得分:1)
我也遇到了这个问题,这就是我最终要做的...
我必须使用此Electron https://fonts.googleapis.com/css?family=Tangerine API才能重试更多次。
您的“登录”事件处理程序将如下所示
const { app, BrowserWindow, ipcMain, net } = require('electron');
app.on("login", (event, webContents, request, authInfo, callback) => {
event.preventDefault();
let username = "";
let password = "";
const req = net.request({
method: request.method,
url: request.url
});
req.on("response", response => {
console.log(response);
if (response.statusCode > 400) {
console.error("something went wrong");
callback("", "");
} else {
console.log("we are good, request was successful"); // the request was authenticated correctly
callback(username, password);
}
response.on("error", err => {
console.error("something went wrong, response error", err);
callback("", "");
});
});
req.on("login", (loginAuthInfo, loginCallback) => {
this.createAuthPrompt().then(credentials => {
username = credentials.username;
password = credentials.password;
loginCallback(username, password);
});
});
req.end();
});