Skip to content

Commit 383fcab

Browse files
author
Tom Kirkpatrick
committed
Initialise middleware components automatically [#6]
1 parent 91e0266 commit 383fcab

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@ An 'access' [Operation Hook](https://docs.strongloop.com/display/public/LB/Opera
3535

3636
4. Create a middleware.json file in your server folder (if you don't already have one).
3737

38-
5. Enable the `loopback#context`, `loopback#token` and `loopback-component-group-access#user-context` middleware.
38+
5. Ensure that you enable the `loopback#context`, `loopback#token` middleware early in your middleware chain.
3939

4040
```json
4141
{
4242
"initial:before": {
4343
"loopback#context": {},
44-
"loopback#token": {},
45-
"loopback-component-group-access#user-context": {}
44+
"loopback#token": {}
4645
},
4746
}
4847
```

lib/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const debug = require('debug')('loopback:componenet:access');
44
const AccessUtils = require('./utils');
5+
const accessLogger = require('./middleware/access-logger');
6+
const userContext = require('./middleware/user-context');
57

68
module.exports = function loopbackComponentAccess(app, options) {
79
debug('initializing component');
@@ -13,6 +15,10 @@ module.exports = function loopbackComponentAccess(app, options) {
1315
throw new Error('loopback-component-access-groups requires loopback 2.0 or newer');
1416
}
1517

18+
// Initialize middleware
19+
app.middleware('initial:before', userContext());
20+
app.middleware('routes:before', accessLogger());
21+
1622
// Initialise helper class.
1723
const accessUtils = new AccessUtils(app, options);
1824

lib/middleware/access-logger.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const debug = require('debug')('loopback:componenet:access:logger');
44

55
module.exports = function accessLoggerMiddleware() {
6+
debug('initializing access logger middleware');
67
return function accessLogger(req, res, next) {
78
if (req.accessToken) {
89
debug('req: %s %s, token: %o', req.method, req.originalUrl, req.accessToken);

lib/middleware/user-context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const loopback = require('loopback');
55
const Promise = require("bluebird");
66

77
module.exports = function userContextMiddleware() {
8+
debug('initializing user context middleware');
89
// set current user to enable user access for remote methods
910
return function userContext(req, res, next) {
1011
const loopbackContext = loopback.getCurrentContext();

test/fixtures/simple-app/server/middleware.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@
22
"initial:before": {
33
"loopback#favicon": {},
44
"loopback#context": {},
5-
"loopback#token": {},
6-
"../../../../lib/middleware/user-context": {}
5+
"loopback#token": {}
76
},
87
"session": {},
98
"auth": {},
109
"parse": {},
11-
"routes:before": {
12-
"../../../../lib/middleware/access-logger": {
13-
"paths": "/api/*"
14-
}
15-
},
1610
"routes": {
1711
"loopback#rest": {
1812
"paths": [

0 commit comments

Comments
 (0)