@apolitical/server 3.0.0-beta.0 → 3.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/src/config.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const { NODE_ENV, LOG_LEVEL } = process.env;
|
|
4
4
|
|
|
5
5
|
const NAME = 'apolitical-server';
|
|
6
|
+
const UUID = '812c9f40-af4b-4718-b41d-54c8a81e148d';
|
|
6
7
|
const VERSION = '2.6.0';
|
|
7
8
|
const ADMIN_ROLE = 'administrator';
|
|
8
9
|
|
|
@@ -53,9 +54,9 @@ module.exports = {
|
|
|
53
54
|
},
|
|
54
55
|
},
|
|
55
56
|
MORGAN_OPTIONS: {
|
|
56
|
-
|
|
57
|
+
LOGGED_OUT_ID: 'logged-out',
|
|
57
58
|
TOKENS: {
|
|
58
|
-
|
|
59
|
+
USER_ID: 'user-id',
|
|
59
60
|
},
|
|
60
61
|
},
|
|
61
62
|
CACHE_OPTIONS: {
|
|
@@ -105,7 +106,7 @@ module.exports = {
|
|
|
105
106
|
DEFAULT_PAYLOAD: {
|
|
106
107
|
role: ADMIN_ROLE,
|
|
107
108
|
admin: true,
|
|
108
|
-
|
|
109
|
+
id: UUID,
|
|
109
110
|
iss: NAME,
|
|
110
111
|
sub: 'login',
|
|
111
112
|
},
|
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module.exports = ({ morgan, config, logger }) => {
|
|
4
4
|
const {
|
|
5
|
-
|
|
6
|
-
TOKENS: {
|
|
5
|
+
LOGGED_OUT_ID,
|
|
6
|
+
TOKENS: { USER_ID },
|
|
7
7
|
} = config.SERVER.MORGAN_OPTIONS;
|
|
8
8
|
|
|
9
|
-
function
|
|
10
|
-
return req.user && req.user.
|
|
9
|
+
function getUserId(req) {
|
|
10
|
+
return req.user && req.user.id ? req.user.id : LOGGED_OUT_ID;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
function buildCustomFormat(tokens, req, res) {
|
|
@@ -15,7 +15,7 @@ module.exports = ({ morgan, config, logger }) => {
|
|
|
15
15
|
tokens.method(req, res),
|
|
16
16
|
tokens.url(req, res),
|
|
17
17
|
tokens.status(req, res),
|
|
18
|
-
tokens[
|
|
18
|
+
tokens[USER_ID](req),
|
|
19
19
|
tokens['response-time'](req, res),
|
|
20
20
|
'ms',
|
|
21
21
|
].join(' ');
|
|
@@ -23,10 +23,10 @@ module.exports = ({ morgan, config, logger }) => {
|
|
|
23
23
|
|
|
24
24
|
function buildMiddleware(labels) {
|
|
25
25
|
// Setup Morgan with custom format and Apolitical Logger stream
|
|
26
|
-
morgan.token(
|
|
26
|
+
morgan.token(USER_ID, getUserId);
|
|
27
27
|
const morganMiddleware = morgan(buildCustomFormat, logger.where(__filename, 'morganMiddleware', labels));
|
|
28
28
|
return morganMiddleware;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
return {
|
|
31
|
+
return { getUserId, buildMiddleware };
|
|
32
32
|
};
|
|
@@ -7,7 +7,7 @@ module.exports =
|
|
|
7
7
|
JWT: { AUTH0 },
|
|
8
8
|
},
|
|
9
9
|
logger,
|
|
10
|
-
loggerHelper: {
|
|
10
|
+
loggerHelper: { getUserId },
|
|
11
11
|
serverError: { GeneralError },
|
|
12
12
|
}) =>
|
|
13
13
|
({ labels, redirectURL, fallbackController }) => {
|
|
@@ -35,12 +35,12 @@ module.exports =
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
// Log the error with all the relevent info
|
|
38
|
-
const
|
|
38
|
+
const userId = getUserId(req);
|
|
39
39
|
if (req.log) {
|
|
40
|
-
req.log.warn(message, { errors,
|
|
40
|
+
req.log.warn(message, { errors, userId });
|
|
41
41
|
} else {
|
|
42
42
|
const errorLogger = logger.where(__filename, 'handler', labels);
|
|
43
|
-
errorLogger.warn(message, { errors,
|
|
43
|
+
errorLogger.warn(message, { errors, userId });
|
|
44
44
|
}
|
|
45
45
|
// Use the redirect URL to redirect the request
|
|
46
46
|
if (redirectURL) {
|