Electron-206 - Implemented basic auth error handler

This commit is contained in:
Kiran Niranjan 2017-11-09 17:31:38 +05:30 committed by Kiran Niranjan
parent cd702ab2c3
commit 0b7bad60e3
4 changed files with 47 additions and 2 deletions

View File

@ -26,6 +26,10 @@
font-size: .9em;
}
.credentials-error {
color: red;
}
form {
padding-top: 15px;
}
@ -60,6 +64,7 @@
<div class="container">
<span>Please provide your login credentials for:</span>
<span id="hostname" class="hostname">hostname</span>
<span id="credentialsError" class="credentials-error">Invalid user name/password</span>
<form id="basicAuth" name="Basic Auth" action="Login">
<table class="form">
<tbody>

View File

@ -45,14 +45,20 @@ function getTemplatePath() {
* Opens the basic auth window for authentication
* @param {String} windowName - name of the window upon which this window should show
* @param {String} hostname - name of the website that requires authentication
* @param {boolean} isValidCredentials - false if invalid username or password
* @param {Function} clearSettings
* @param {Function} callback
*/
function openBasicAuthWindow(windowName, hostname, callback) {
function openBasicAuthWindow(windowName, hostname, isValidCredentials, clearSettings, callback) {
// Register callback function
if (typeof callback === 'function') {
local.authCallback = callback;
}
// Register close function
if (typeof clearSettings === 'function') {
local.clearSettings = clearSettings;
}
// This prevents creating multiple instances of the
// basic auth window
@ -89,6 +95,7 @@ function openBasicAuthWindow(windowName, hostname, callback) {
basicAuthWindow.webContents.on('did-finish-load', () => {
basicAuthWindow.webContents.send('hostname', hostname);
basicAuthWindow.webContents.send('isValidCredentials', isValidCredentials);
});
basicAuthWindow.on('close', () => {
@ -108,6 +115,10 @@ ipc.on('login', (event, args) => {
});
ipc.on('close-basic-auth', () => {
if (typeof local.clearSettings === 'function') {
local.clearSettings();
}
if (basicAuthWindow) {
basicAuthWindow.close();
}

View File

@ -52,4 +52,15 @@ ipc.on('hostname', (event, host) => {
if (hostname){
hostname.innerHTML = host || 'unknown';
}
});
/**
* Triggered if user credentials are invalid
*/
ipc.on('isValidCredentials', (event, isValidCredentials) => {
let credentialsError = document.getElementById('credentialsError');
if (credentialsError){
credentialsError.style.display = isValidCredentials ? 'none' : 'block'
}
});

View File

@ -3,6 +3,8 @@
const electron = require('electron');
const basicAuth = require('../basicAuth');
let currentAuthURL;
let tries = 0;
/**
* Having a proxy or hosts that requires authentication will allow user to
@ -12,14 +14,30 @@ electron.app.on('login', (event, webContents, request, authInfo, callback) => {
event.preventDefault();
if(currentAuthURL !== request.url) {
currentAuthURL = request.url;
} else {
tries++
}
// name of the host to display
let hostname = authInfo.host || authInfo.realm;
let browserWin = electron.BrowserWindow.fromWebContents(webContents);
let windowName = browserWin.winName || '';
/**
* Method that resets currentAuthURL and tries
* if user closes the auth window
*/
function clearSettings() {
callback();
currentAuthURL = '';
tries = 0;
}
/**
* Opens an electron modal window in which
* user can enter credentials fot the host
*/
basicAuth.openBasicAuthWindow(windowName, hostname, callback);
basicAuth.openBasicAuthWindow(windowName, hostname, tries === 0, clearSettings, callback);
});