Skip to content

Commit

Permalink
Add the whoami smart action and an exemple to extend the API
Browse files Browse the repository at this point in the history
  • Loading branch information
SeyZ committed Mar 22, 2018
1 parent 4174fa0 commit a31b502
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
7 changes: 5 additions & 2 deletions app.js
Expand Up @@ -28,8 +28,11 @@ app.use(jwt({
credentialsRequired: false
}));


app.use('/forest', require('./decorators/routes/products.js'));
fs.readdirSync('./decorators/routes').forEach((file) => {
if (file[0] !== '.') {
app.use('/forest', require(`./decorators/routes/${file}`));
}
});

fs.readdirSync('./routes').forEach((file) => {
if (file[0] !== '.') {
Expand Down
Binary file modified database.dump
Binary file not shown.
15 changes: 15 additions & 0 deletions decorators/routes/companies.js
@@ -0,0 +1,15 @@
const express = require('express');
const router = express.Router();
const Liana = require('forest-express-sequelize');

router.delete('/companies/:companyId', Liana.ensureAuthenticated,
(req, res, next) => {
if (req.user.data.teams.indexOf('Management') > -1) {
next();
} else {
res.status(403).send('Sorry, you\'re now allowed to delete a company. Ask someone in the Management team.');
}
});

module.exports = router;

7 changes: 6 additions & 1 deletion forest/companies.js
Expand Up @@ -37,5 +37,10 @@ Liana.collection('companies', {
type: 'File',
isRequired: true
}]
}],
}, {
name: 'Whoami',
type: 'global',
endpoint: '/forest/whoami',
httpMethod: 'GET'
}],
});
9 changes: 9 additions & 0 deletions routes/whoami.js
@@ -0,0 +1,9 @@
const express = require('express');
const router = express.Router();
const Liana = require('forest-express-sequelize');

router.get('/whoami', Liana.ensureAuthenticated, (req, res) => {
res.send({ success: `You are ${req.user.data.first_name} ${req.user.data.last_name}.` });
});

module.exports = router;

0 comments on commit a31b502

Please sign in to comment.