diff --git a/src/api.js b/src/api.js deleted file mode 100644 index 7994d10597..0000000000 --- a/src/api.js +++ /dev/null @@ -1,30 +0,0 @@ -import Cookies from 'universal-cookie'; -import superagentPromise from 'superagent-promise'; -import _superagent from 'superagent'; -import qs from 'qs'; - -const cookies = new Cookies(); -const superagent = superagentPromise(_superagent, global.Promise); -const responseBody = res => res.body; - -const setJwt = () => { - const jwt = cookies.get('token'); - return jwt ? { 'Authorization': `JWT ${jwt}` } : {} -}; - -const requests = { - get: (url, params) => { - const query = qs.stringify(params, { addQueryPrefix: true }); - return superagent.get(`${url}${query}`).set(setJwt()).then(responseBody); - }, - - post: (url, body) => - superagent.post(`${url}`, body).set(setJwt()).then(responseBody), - - put: (url, body) => - superagent.put(`${url}`, body).set(setJwt()).then(responseBody) -}; - -export default { - requests -}; diff --git a/src/index.js b/src/index.js index d89a9373ab..d95c30b6b3 100644 --- a/src/index.js +++ b/src/index.js @@ -11,16 +11,14 @@ import autopopulate from './plugins/autopopulate'; import paginate from './plugins/paginate'; import buildQuery from './plugins/buildQuery'; import internationalization from './plugins/internationalization'; -import bindModel from './middleware/bindModel'; -import locale from './middleware/locale'; +import { bindModel, locale, checkRole } from './middleware'; import { query, create, findOne, destroy, update } from './requestHandlers'; import { schemaBaseFields } from './helpers/mongoose/schemaBaseFields'; import fieldToSchemaMap from './helpers/mongoose/fieldToSchemaMap'; import authValidate from './auth/validate'; import authRequestHandlers from './auth/requestHandlers'; -import middleware from './middleware'; import passwordResetConfig from './auth/passwordResets/passwordReset.config'; -import passportLocalMongoose from 'passport-local-mongoose'; +import validateConfig from './lib/validateConfig'; class Payload { @@ -70,6 +68,7 @@ class Payload { // TODO: Build safe config before initializing models and routes options.models && options.models.forEach(config => { + validateConfig(config, this.models); // TODO: consider making schemaBaseFields a mongoose plugin for consistency const fields = { ...schemaBaseFields }; @@ -118,7 +117,7 @@ class Payload { options.config.roles.forEach((role) => { options.router .route(`/role/${role}`) - .get(passport.authenticate(config.auth.strategy, { session: false }), middleware.role(role), auth.me); + .get(passport.authenticate(config.auth.strategy, { session: false }), checkRole(role), auth.me); }); // password resets diff --git a/src/lib/validateConfig.js b/src/lib/validateConfig.js new file mode 100644 index 0000000000..e4b4de3adc --- /dev/null +++ b/src/lib/validateConfig.js @@ -0,0 +1,6 @@ +export default function validateConfig(config, models) { + if (models[config.labels.singular]) { + throw new Error('Model name "' + config.labels.singular + '" is already in use'); + } + // TODO: Come up with a lot more things to check for and throw errors about +} diff --git a/src/middleware/bindModel.js b/src/middleware/bindModel.js index 5e0ccf4650..3c00661283 100644 --- a/src/middleware/bindModel.js +++ b/src/middleware/bindModel.js @@ -3,6 +3,6 @@ const bindModel = model => { req.model = model; next(); }; -} +}; export default bindModel; diff --git a/src/middleware/checkRole.js b/src/middleware/checkRole.js new file mode 100644 index 0000000000..ced3e53453 --- /dev/null +++ b/src/middleware/checkRole.js @@ -0,0 +1,6 @@ +export default function checkRole(role) { + return function (req, res, next) { + if (role !== req.user.role) res.status(401).send('Role not authorized.'); + else next(); + } +} diff --git a/src/middleware/index.js b/src/middleware/index.js index ea5b40944e..8e42bae334 100644 --- a/src/middleware/index.js +++ b/src/middleware/index.js @@ -1,7 +1,3 @@ -import role from './role'; -import bindModel from './bindModel'; - -export default { - bindModel, - role -}; +export {default as bindModel} from './bindModel'; +export {default as checkRole} from './checkRole'; +export {default as locale} from './locale'; diff --git a/src/middleware/locale.js b/src/middleware/locale.js index f7560003ad..798de157b9 100644 --- a/src/middleware/locale.js +++ b/src/middleware/locale.js @@ -6,7 +6,6 @@ import languageParser from 'accept-language-parser'; * @param localization * @returns {Function} */ - export default function locale(localization) { return function (req, res, next) { let setLocale; diff --git a/src/middleware/role.js b/src/middleware/role.js deleted file mode 100644 index 8fc4f5e0da..0000000000 --- a/src/middleware/role.js +++ /dev/null @@ -1,8 +0,0 @@ -function role(allowedRole) { - return function (req, res, next) { - if (allowedRole !== req.user.role) res.status(401).send('Role not authorized.'); - else next(); - } -} - -export default role;