Skip to content

Commit 22471d6

Browse files
committed
Backend: auth for config and queries. Refactor of middleware exports
1 parent e3f1679 commit 22471d6

File tree

10 files changed

+43
-31
lines changed

10 files changed

+43
-31
lines changed

api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { join } = require('path');
22
const express = require('express');
3-
const errorHandler = require('./middleware/errorHandler');
3+
const { errorHandler } = require('./middleware');
44

55
const api = express();
66

controllers/apps/createApp.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
const asyncWrapper = require('../../middleware/asyncWrapper');
22
const App = require('../../models/App');
33
const loadConfig = require('../../utils/loadConfig');
4-
const ErrorResponse = require('../../utils/ErrorResponse');
54

65
// @desc Create new app
76
// @route POST /api/apps
87
// @access Public
98
const createApp = asyncWrapper(async (req, res, next) => {
10-
if (!req.isAuthenticated) {
11-
return next(new ErrorResponse('Unauthorized', 401));
12-
}
13-
149
const { pinAppsByDefault } = await loadConfig();
1510

1611
let app;

controllers/apps/deleteApp.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
const asyncWrapper = require('../../middleware/asyncWrapper');
22
const App = require('../../models/App');
3-
const ErrorResponse = require('../../utils/ErrorResponse');
43

54
// @desc Delete app
65
// @route DELETE /api/apps/:id
76
// @access Public
87
const deleteApp = asyncWrapper(async (req, res, next) => {
9-
if (!req.isAuthenticated) {
10-
return next(new ErrorResponse('Unauthorized', 401));
11-
}
12-
138
await App.destroy({
149
where: { id: req.params.id },
1510
});

controllers/apps/reorderApps.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
const asyncWrapper = require('../../middleware/asyncWrapper');
22
const App = require('../../models/App');
3-
const ErrorResponse = require('../../utils/ErrorResponse');
43

54
// @desc Reorder apps
65
// @route PUT /api/apps/0/reorder
76
// @access Public
87
const reorderApps = asyncWrapper(async (req, res, next) => {
9-
if (!req.isAuthenticated) {
10-
return next(new ErrorResponse('Unauthorized', 401));
11-
}
12-
138
req.body.apps.forEach(async ({ id, orderId }) => {
149
await App.update(
1510
{ orderId },

controllers/apps/updateApp.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
const asyncWrapper = require('../../middleware/asyncWrapper');
22
const App = require('../../models/App');
3-
const ErrorResponse = require('../../utils/ErrorResponse');
43

54
// @desc Update app
65
// @route PUT /api/apps/:id
76
// @access Public
87
const updateApp = asyncWrapper(async (req, res, next) => {
9-
if (!req.isAuthenticated) {
10-
return next(new ErrorResponse('Unauthorized', 401));
11-
}
12-
138
let app = await App.findOne({
149
where: { id: req.params.id },
1510
});

middleware/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
asyncWrapper: require('./asyncWrapper'),
3+
auth: require('./auth'),
4+
errorHandler: require('./errorHandler'),
5+
upload: require('./multer'),
6+
requireAuth: require('./requireAuth'),
7+
requireBody: require('./requireBody'),
8+
};

middleware/requireAuth.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const ErrorResponse = require('../utils/ErrorResponse');
2+
3+
const requireAuth = (req, res, next) => {
4+
if (!req.isAuthenticated) {
5+
return next(new ErrorResponse('Unauthorized', 401));
6+
}
7+
8+
next();
9+
};
10+
11+
module.exports = requireAuth;

routes/apps.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const express = require('express');
22
const router = express.Router();
3-
const upload = require('../middleware/multer');
4-
const auth = require('../middleware/auth');
3+
4+
// middleware
5+
const { auth, requireAuth, upload } = require('../middleware');
56

67
const {
78
createApp,
@@ -12,14 +13,17 @@ const {
1213
reorderApps,
1314
} = require('../controllers/apps');
1415

15-
router.route('/').post(auth, upload, createApp).get(auth, getAllApps);
16+
router
17+
.route('/')
18+
.post(auth, requireAuth, upload, createApp)
19+
.get(auth, getAllApps);
1620

1721
router
1822
.route('/:id')
1923
.get(auth, getSingleApp)
20-
.put(auth, upload, updateApp)
21-
.delete(auth, deleteApp);
24+
.put(auth, requireAuth, upload, updateApp)
25+
.delete(auth, requireAuth, deleteApp);
2226

23-
router.route('/0/reorder').put(auth, reorderApps);
27+
router.route('/0/reorder').put(auth, requireAuth, reorderApps);
2428

2529
module.exports = router;

routes/config.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
const express = require('express');
22
const router = express.Router();
33

4+
// middleware
5+
const { auth, requireAuth } = require('../middleware');
6+
47
const {
58
getCSS,
69
updateCSS,
710
getConfig,
811
updateConfig,
912
} = require('../controllers/config');
1013

11-
router.route('/').get(getConfig).put(updateConfig);
14+
router.route('/').get(getConfig).put(auth, requireAuth, updateConfig);
1215

13-
router.route('/0/css').get(getCSS).put(updateCSS);
16+
router.route('/0/css').get(getCSS).put(auth, requireAuth, updateCSS);
1417

1518
module.exports = router;

routes/queries.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
const express = require('express');
22
const router = express.Router();
33

4+
// middleware
5+
const { auth, requireAuth } = require('../middleware');
6+
47
const {
58
getQueries,
69
addQuery,
710
deleteQuery,
811
updateQuery,
912
} = require('../controllers/queries/');
1013

11-
router.route('/').post(addQuery).get(getQueries);
12-
router.route('/:prefix').delete(deleteQuery).put(updateQuery);
14+
router.route('/').post(auth, requireAuth, addQuery).get(getQueries);
15+
router
16+
.route('/:prefix')
17+
.delete(auth, requireAuth, deleteQuery)
18+
.put(auth, requireAuth, updateQuery);
1319

1420
module.exports = router;

0 commit comments

Comments
 (0)