revises the way that the config sends back collections and globals

This commit is contained in:
James
2019-12-21 20:00:31 -05:00
parent 9e229c70c5
commit dcfa909a35
3 changed files with 38 additions and 10 deletions

View File

@@ -30,7 +30,7 @@ export default User => ({
* @returns {*}
*/
login: (req, res) => {
let { email, password } = req.body;
const { email, password } = req.body;
User.findByUsername(email, (err, user) => {
if (err || !user) return res.status(401).json({ message: 'Auth Failed' });
@@ -38,15 +38,15 @@ export default User => ({
user.authenticate(password, (authErr, model, passwordError) => {
if (authErr || passwordError) return res.status(401).json({ message: 'Auth Failed' });
let opts = {};
opts.expiresIn = process.env.tokenExpiration || 7200; // 20min default expiration
const opts = {};
opts.expiresIn = process.env.tokenExpiration || 7200; // 20min default expiration
const secret = process.env.secret || 'SECRET_KEY';
const token = jwt.sign({ email }, secret, opts);
return res.status(200).json({
message: 'Auth Passed',
token
token,
});
})
});
});
},
@@ -74,5 +74,5 @@ export default User => ({
}
next();
}
})
},
});

View File

@@ -26,7 +26,7 @@ class Payload {
initUploads(options);
initPassport(this.app);
initCORS(options);
registerConfigRoute(options);
registerConfigRoute(options, this.getCollections, this.getGlobals);
if (!this.config.disableAdmin) {
initWebpack(options);
@@ -44,6 +44,10 @@ class Payload {
this.globals = registerGlobalSchema(globals, this.config);
registerGlobalRoutes(this.globals.model, this.router);
}
getCollections = () => this.collections;
getGlobals = () => this.globals;
}
module.exports = Payload;

View File

@@ -1,10 +1,34 @@
import passport from 'passport';
const registerConfigRoute = ({ router, app, config }) => {
const registerConfigRoute = ({ router, config }, getCollections, getGlobals) => {
router.use('/config',
passport.authenticate('jwt', { session: false }),
(req, res) => {
res.json(config);
const registeredCollections = getCollections();
const globals = getGlobals();
const collections = {};
const contentBlocks = {};
Object.keys(registeredCollections).forEach((key) => {
const fullConfig = registeredCollections[key].config;
const collectionConfig = { ...fullConfig };
delete collectionConfig.plugins;
delete collectionConfig.policies;
if (collectionConfig.useAsContentBlock) {
contentBlocks[collectionConfig.slug] = collectionConfig;
}
collections[collectionConfig.slug] = collectionConfig;
});
res.json({
...config,
collections,
globals,
contentBlocks,
});
});
};