X-Git-Url: https://git.squeep.com/?p=squeep-authentication-module;a=blobdiff_plain;f=lib%2Ftemplate%2Fhelpers.js;fp=lib%2Ftemplate%2Fhelpers.js;h=67162f867f8ae784e215b43a829ed5c99b05fd70;hp=0000000000000000000000000000000000000000;hb=2c3ddf0a6f40b9d0a4e54fa12b84b8af33eaaadc;hpb=54ca04e330d14a12344ddc2b161c1d9b55bfd7d7 diff --git a/lib/template/helpers.js b/lib/template/helpers.js new file mode 100644 index 0000000..67162f8 --- /dev/null +++ b/lib/template/helpers.js @@ -0,0 +1,46 @@ +'use strict'; + +/** + * Populates nevLinks with (currently hardcoded) session-related links. + * @param {Number} pagePathLevel relative to base + * @param {Object} ctx + * @param {String=} ctx.url redirect on logout + * @param {Object=} ctx.session + * @param {String=} ctx.session.authenticatedIdentifier + * @param {String=} ctx.session.authenticatedProfile + * @param {Object} options + * @param {Object[]=} options.navLinks created if not present + * @param {String=} options.pageIdentifier + */ +function sessionNavLinks(pagePathLevel, ctx, options) { + if (!options.navLinks) { + options.navLinks = []; + } + const rootPath = '../'.repeat(pagePathLevel); + const redirectQuery = ctx?.url ? `?r=${encodeURIComponent(ctx.url)}` : rootPath; + const adminPath = rootPath + 'admin/'; + + const user = ctx?.session?.authenticatedProfile || ctx?.session?.authenticatedIdentifier; + if (user) { + if (options.pageIdentifier !== 'account') { + options.navLinks.push({ + text: 'Account', + href: `${adminPath}settings`, + }); + } + options.navLinks.push({ + text: `Logout (${user})`, + href: `${adminPath}logout${redirectQuery}`, + }); + } else { + options.navLinks.push({ + text: 'Login', + href: `${adminPath}login${redirectQuery}`, + }); + } +} + + +module.exports = { + sessionNavLinks, +};