Backend: auth for config and queries. Refactor of middleware exports

This commit is contained in:
Paweł Malak 2021-11-11 16:18:31 +01:00
parent e3f167921c
commit 22471d64c7
10 changed files with 43 additions and 31 deletions

2
api.js
View file

@ -1,6 +1,6 @@
const { join } = require('path'); const { join } = require('path');
const express = require('express'); const express = require('express');
const errorHandler = require('./middleware/errorHandler'); const { errorHandler } = require('./middleware');
const api = express(); const api = express();

View file

@ -1,16 +1,11 @@
const asyncWrapper = require('../../middleware/asyncWrapper'); const asyncWrapper = require('../../middleware/asyncWrapper');
const App = require('../../models/App'); const App = require('../../models/App');
const loadConfig = require('../../utils/loadConfig'); const loadConfig = require('../../utils/loadConfig');
const ErrorResponse = require('../../utils/ErrorResponse');
// @desc Create new app // @desc Create new app
// @route POST /api/apps // @route POST /api/apps
// @access Public // @access Public
const createApp = asyncWrapper(async (req, res, next) => { const createApp = asyncWrapper(async (req, res, next) => {
if (!req.isAuthenticated) {
return next(new ErrorResponse('Unauthorized', 401));
}
const { pinAppsByDefault } = await loadConfig(); const { pinAppsByDefault } = await loadConfig();
let app; let app;

View file

@ -1,15 +1,10 @@
const asyncWrapper = require('../../middleware/asyncWrapper'); const asyncWrapper = require('../../middleware/asyncWrapper');
const App = require('../../models/App'); const App = require('../../models/App');
const ErrorResponse = require('../../utils/ErrorResponse');
// @desc Delete app // @desc Delete app
// @route DELETE /api/apps/:id // @route DELETE /api/apps/:id
// @access Public // @access Public
const deleteApp = asyncWrapper(async (req, res, next) => { const deleteApp = asyncWrapper(async (req, res, next) => {
if (!req.isAuthenticated) {
return next(new ErrorResponse('Unauthorized', 401));
}
await App.destroy({ await App.destroy({
where: { id: req.params.id }, where: { id: req.params.id },
}); });

View file

@ -1,15 +1,10 @@
const asyncWrapper = require('../../middleware/asyncWrapper'); const asyncWrapper = require('../../middleware/asyncWrapper');
const App = require('../../models/App'); const App = require('../../models/App');
const ErrorResponse = require('../../utils/ErrorResponse');
// @desc Reorder apps // @desc Reorder apps
// @route PUT /api/apps/0/reorder // @route PUT /api/apps/0/reorder
// @access Public // @access Public
const reorderApps = asyncWrapper(async (req, res, next) => { const reorderApps = asyncWrapper(async (req, res, next) => {
if (!req.isAuthenticated) {
return next(new ErrorResponse('Unauthorized', 401));
}
req.body.apps.forEach(async ({ id, orderId }) => { req.body.apps.forEach(async ({ id, orderId }) => {
await App.update( await App.update(
{ orderId }, { orderId },

View file

@ -1,15 +1,10 @@
const asyncWrapper = require('../../middleware/asyncWrapper'); const asyncWrapper = require('../../middleware/asyncWrapper');
const App = require('../../models/App'); const App = require('../../models/App');
const ErrorResponse = require('../../utils/ErrorResponse');
// @desc Update app // @desc Update app
// @route PUT /api/apps/:id // @route PUT /api/apps/:id
// @access Public // @access Public
const updateApp = asyncWrapper(async (req, res, next) => { const updateApp = asyncWrapper(async (req, res, next) => {
if (!req.isAuthenticated) {
return next(new ErrorResponse('Unauthorized', 401));
}
let app = await App.findOne({ let app = await App.findOne({
where: { id: req.params.id }, where: { id: req.params.id },
}); });

8
middleware/index.js Normal file
View file

@ -0,0 +1,8 @@
module.exports = {
asyncWrapper: require('./asyncWrapper'),
auth: require('./auth'),
errorHandler: require('./errorHandler'),
upload: require('./multer'),
requireAuth: require('./requireAuth'),
requireBody: require('./requireBody'),
};

11
middleware/requireAuth.js Normal file
View file

@ -0,0 +1,11 @@
const ErrorResponse = require('../utils/ErrorResponse');
const requireAuth = (req, res, next) => {
if (!req.isAuthenticated) {
return next(new ErrorResponse('Unauthorized', 401));
}
next();
};
module.exports = requireAuth;

View file

@ -1,7 +1,8 @@
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
const upload = require('../middleware/multer');
const auth = require('../middleware/auth'); // middleware
const { auth, requireAuth, upload } = require('../middleware');
const { const {
createApp, createApp,
@ -12,14 +13,17 @@ const {
reorderApps, reorderApps,
} = require('../controllers/apps'); } = require('../controllers/apps');
router.route('/').post(auth, upload, createApp).get(auth, getAllApps); router
.route('/')
.post(auth, requireAuth, upload, createApp)
.get(auth, getAllApps);
router router
.route('/:id') .route('/:id')
.get(auth, getSingleApp) .get(auth, getSingleApp)
.put(auth, upload, updateApp) .put(auth, requireAuth, upload, updateApp)
.delete(auth, deleteApp); .delete(auth, requireAuth, deleteApp);
router.route('/0/reorder').put(auth, reorderApps); router.route('/0/reorder').put(auth, requireAuth, reorderApps);
module.exports = router; module.exports = router;

View file

@ -1,6 +1,9 @@
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
// middleware
const { auth, requireAuth } = require('../middleware');
const { const {
getCSS, getCSS,
updateCSS, updateCSS,
@ -8,8 +11,8 @@ const {
updateConfig, updateConfig,
} = require('../controllers/config'); } = require('../controllers/config');
router.route('/').get(getConfig).put(updateConfig); router.route('/').get(getConfig).put(auth, requireAuth, updateConfig);
router.route('/0/css').get(getCSS).put(updateCSS); router.route('/0/css').get(getCSS).put(auth, requireAuth, updateCSS);
module.exports = router; module.exports = router;

View file

@ -1,6 +1,9 @@
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
// middleware
const { auth, requireAuth } = require('../middleware');
const { const {
getQueries, getQueries,
addQuery, addQuery,
@ -8,7 +11,10 @@ const {
updateQuery, updateQuery,
} = require('../controllers/queries/'); } = require('../controllers/queries/');
router.route('/').post(addQuery).get(getQueries); router.route('/').post(auth, requireAuth, addQuery).get(getQueries);
router.route('/:prefix').delete(deleteQuery).put(updateQuery); router
.route('/:prefix')
.delete(auth, requireAuth, deleteQuery)
.put(auth, requireAuth, updateQuery);
module.exports = router; module.exports = router;