User-authentication.js Help

#1

I don't know if it's a limitation of user-authentication.js or if I'm doing something wrong (most likely the latter). I'm trying to modify my admin UI LDAP authentication plugin to use promises since the Pi can't seem to handle the request in time for everything to load (the same code running on a proper server works fine). It works without the promises (after I restart the service once after boot) but as soon as I put the promise code in the username/password boxes disappear from the admin UI login page. I'm not seeing any errors in the console and I put in some console.log lines to verify the module.exports = { code is actually running.

"use strict";
const LdapAuth = require('ldapauth-fork');
const util = require('util');
const fs = require('fs');
const server = ""; /*Use DC FQDN*/
const port = 636;
const protocol = "ldaps://";
const certificatepath = "";
const bindusername = "";
const bindpassword = '';
const searchbase = "";
const filter = "(SAMAccountName={{username}})";
const config = {
    ldap: {
        url: protocol + server + ":" + port,
        adminDn: bindusername,
        adminPassword: bindpassword,
        searchBase: searchbase,
        searchFilter: filter,
        timeout: 2000, /*Doesn't seem to do anything*/
        idleTimeout: 30000, /*Doesn't seem to do anything*/
        tlsOptions: {ca: [ fs.readFileSync(certificatepath) ]},
        reconnect: false /*Doesn't seem to do anything*/
    }
};

var validusernames = [];

function ldapconnect(config){
    var ldapconnection = new LdapAuth({
        url: config.ldap.url,
        bindDN: config.ldap.adminDn,
        bindCredentials: config.ldap.adminPassword,
        searchBase: config.ldap.searchBase,
        searchFilter: config.ldap.searchFilter,
        timeout: config.ldap.timeout,
        tlsOptions: config.ldap.tlsOptions,
        idleTimeout: config.ldap.idleTimeout
    });
    
    return ldapconnection;
}

function getADUsers(){
	return new Promise((resolve, reject) => {
		var ldap = ldapconnect(config);

		/*Set up error trap for connection resets - shouldn't occur while closing the LDAP connection after authentication, but the Docker image doesn't seem to play nice with this*/
		ldap.on('error', function(err) {
			if (err != "Error: read ECONNRESET"){/*Gets lots of "Error: This socket is closed" messages in the log if a RST is received when using TLS - might want to disable closing completely*/
				ldap.close();
			}
			console.log("LDAP error detected");
			console.log(err);
		});

		const nrgroupsearchbase = "";
		const options = {
			filter:"(&(objectCategory=user)(memberOf=))",
			scope:"sub"
		};

		ldap._search(nrgroupsearchbase, options, function(err, ldapresult) {
			if (err) {
				var errstr = util.format(" LDAP error in get usernames: %s", err);
				reject(errstr);
			}
			else{
				for (var user in ldapresult){
					validusernames[user] = ldapresult[user].sAMAccountName;
				}
				resolve(validusernames);
			}
		});

		ldap.close();
	});
}

var interval = setInterval(function () { getADUsers(); }, 15 * 60 * 1000); /*Update valid AD users every 15 mins*/

new Promise((resolve, reject) => {
	getADUsers().then(function (){
		resolve();
	});
}).then( function (){
	console.log(validusernames);
	console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	module.exports = {
		type: "credentials",
		users: function(username) {
			return new Promise(function(resolve) {
				var authtrue = false;

				for(var i=0;i<validusernames.length; i++){
					if (validusernames[i] == username) {
						var userauth = { username: username, permissions: "*" };
						authtrue = true;
						resolve(userauth);
					}
				}
				if (!authtrue){
					resolve(null);
				}
			});
		},
		authenticate: function(username,password) {
			return new Promise(function(resolve) {
				var ldap = ldapconnect(config);
				
				/*Set up error trap for connection resets - shouldn't occur while closing the LDAP connection after authentication, but the Docker image doesn't seem to play nice with this*/
				ldap.on('error', function(err) {
					if (err != "Error: read ECONNRESET"){/*Gets lots of "Error: This socket is closed" messages in the log if a RST is received when using TLS - might want to disable closing completely*/
						ldap.close();
					}
					console.log("LDAP error detected");
					console.log(err);
				});
			   
				ldap.authenticate(username, password, function(err, ldapuser) {
					if (err) {
						var errstr = util.format(" LDAP error in auth: %s", err);
						console.log(errstr);
						resolve(null);
					}
					else{
						var user = { username: username, permissions: "*" };
						resolve(user);
					}
				});
				ldap.close();
			});
		}
	};
}).catch(function (err){
	var errstr = util.format(" LDAP error at end: %s", err);
	console.log(errstr);
});