display flash errors for external auths like saml or oidc on the login screen #6154

Signed-off-by: si458 <simonsmith5521@gmail.com>

si458 committed Jun 11, 2024 at 20:06 UTC d7341ab153ba5a4eb9f20a9ef2f8d1cc650f84c3
5 files changed +62 -1
common.js
+15
@@ -404,4 +404,19 @@ module.exports.convertStrArray = function (object, split) {
404 } else {
405 return []
406 }
407 +}
408 +
409 +module.exports.uniqueArray = function (a) {
410 + var seen = {};
411 + var out = [];
412 + var len = a.length;
413 + var j = 0;
414 + for(var i = 0; i < len; i++) {
415 + var item = a[i];
416 + if(seen[item] !== 1) {
417 + seen[item] = 1;
418 + out[j++] = item;
419 + }
420 + }
421 + return out;
422 }
\ No newline at end of file
views/login-mobile.handlebars
+13
@@ -356,6 +356,19 @@
356 }
357 }
358
359 + // Display flash error Messages
360 + var flashErrors = JSON.parse('{{{flashErrors}}}');
361 + if (flashErrors && (flashErrors.length > 0)) {
362 + var msg = '';
363 + for (i = 0; i < flashErrors.length; i++) {
364 + if (flashErrors[i]) {
365 + msg += '<span class="msg error"><b style=color:#8C001A>' + flashErrors[i] + '<b></span><br /><br />';
366 + }
367 + }
368 + QH('message1', msg);
369 + QV('message1', true);
370 + }
371 +
372 // If URL arguments are provided, add them to form posts
373 if (window.location.href.indexOf('?') > 0) {
374 var urlargs = window.location.href.substring(window.location.href.indexOf('?'));
views/login.handlebars
+13
@@ -361,6 +361,19 @@
361 }
362 }
363
364 + // Display flash error Messages
365 + var flashErrors = JSON.parse('{{{flashErrors}}}');
366 + if (flashErrors && (flashErrors.length > 0)) {
367 + var msg = '';
368 + for (i = 0; i < flashErrors.length; i++) {
369 + if (flashErrors[i]) {
370 + msg += '<span class="msg error"><b style=color:#8C001A>' + flashErrors[i] + '<b></span><br /><br />';
371 + }
372 + }
373 + QH('message1', msg);
374 + QV('message1', true);
375 + }
376 +
377 // Fix links if a loginKey if used
378 var urlargs = parseUriArgs();
379 if (urlargs.key) {
views/login2.handlebars
+13
@@ -437,6 +437,19 @@
437 }
438 }
439
440 + // Display flash error Messages
441 + var flashErrors = JSON.parse('{{{flashErrors}}}');
442 + if (flashErrors && (flashErrors.length > 0)) {
443 + var msg = '';
444 + for (i = 0; i < flashErrors.length; i++) {
445 + if (flashErrors[i]) {
446 + msg += '<span class="msg error"><b style=color:#8C001A>' + flashErrors[i] + '<b></span><br /><br />';
447 + }
448 + }
449 + QH('message1', msg);
450 + QV('message1', true);
451 + }
452 +
453 // Fix links if a loginKey if used
454 var urlargs = parseUriArgs();
455 //if (urlargs.key) { Q('termsLinkFooter').href += '?key=' + urlargs.key; }
webserver.js
+8 -1
@@ -3359,6 +3359,12 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
3359 newAccountCaptchaImage = 'newAccountCaptcha.ashx?x=' + newAccountCaptcha;
3360 }
3361
3362 + // Check for flash errors from passport.js and make the array unique
3363 + var flashErrors = [];
3364 + if (req.session.flash && req.session.flash.error) {
3365 + flashErrors = obj.common.uniqueArray(req.session.flash.error);
3366 + }
3367 +
3368 // Render the login page
3369 render(req, res,
3370 getRenderPage((domain.sitestyle == 2) ? 'login2' : 'login', req, domain),
@@ -3380,6 +3386,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
3386 footer: (domain.loginfooter == null) ? '' : domain.loginfooter,
3387 hkey: encodeURIComponent(hardwareKeyChallenge).replace(/'/g, '%27'),
3388 messageid: msgid,
3389 + flashErrors: JSON.stringify(flashErrors),
3390 passhint: passhint,
3391 welcometext: domain.welcometext ? encodeURIComponent(domain.welcometext).split('\'').join('\\\'') : null,
3392 welcomePictureFullScreen: ((typeof domain.welcomepicturefullscreen == 'boolean') ? domain.welcomepicturefullscreen : false),
@@ -6766,7 +6773,6 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
6773 if ((domain.authstrategies.authStrategyFlags & domainAuthStrategyConsts.oidc) != 0) {
6774 let authURL = url + 'auth-oidc'
6775 parent.authLog('setupHTTPHandlers', `OIDC: Authorization URL: ${authURL}`);
6769 - obj.app.use(require('connect-flash')());
6776 obj.app.get(authURL, function (req, res, next) {
6777 var domain = getDomain(req);
6778 if (domain.passport == null) { next(); return; }
@@ -7180,6 +7186,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
7186 passport.serializeUser(function (user, done) { done(null, user.sid); });
7187 passport.deserializeUser(function (sid, done) { done(null, { sid: sid }); });
7188 obj.app.use(passport.initialize());
7189 + obj.app.use(require('connect-flash')());
7190
7191 // Twitter
7192 if ((typeof domain.authstrategies.twitter == 'object') && (typeof domain.authstrategies.twitter.clientid == 'string') && (typeof domain.authstrategies.twitter.clientsecret == 'string')) {