support interaction between module and apps for updating templates before rendering
[squeep-authentication-module] / lib / template / helpers.js
1 'use strict';
2
3 /**
4 * Populates nevLinks with (currently hardcoded) session-related links.
5 * @param {Number} pagePathLevel relative to base
6 * @param {Object} ctx
7 * @param {String=} ctx.url redirect on logout
8 * @param {Object=} ctx.session
9 * @param {String=} ctx.session.authenticatedIdentifier
10 * @param {String=} ctx.session.authenticatedProfile
11 * @param {Object} options
12 * @param {Object[]=} options.navLinks created if not present
13 * @param {String=} options.pageIdentifier
14 */
15 function sessionNavLinks(pagePathLevel, ctx, options) {
16 if (!options.navLinks) {
17 options.navLinks = [];
18 }
19 const rootPath = '../'.repeat(pagePathLevel);
20 const redirectQuery = ctx?.url ? `?r=${encodeURIComponent(ctx.url)}` : rootPath;
21 const adminPath = rootPath + 'admin/';
22
23 const user = ctx?.session?.authenticatedProfile || ctx?.session?.authenticatedIdentifier;
24 if (user) {
25 if (options.pageIdentifier !== 'account') {
26 options.navLinks.push({
27 text: 'Account',
28 href: `${adminPath}settings`,
29 });
30 }
31 options.navLinks.push({
32 text: `Logout (${user})`,
33 href: `${adminPath}logout${redirectQuery}`,
34 });
35 } else {
36 options.navLinks.push({
37 text: 'Login',
38 href: `${adminPath}login${redirectQuery}`,
39 });
40 }
41 }
42
43
44 module.exports = {
45 sessionNavLinks,
46 };