|
|
@@ -1,16 +1,20 @@
|
|
|
-function routeFactory({ dbs, yup, log }) {
|
|
|
+import { authMiddleware, isInGroup } from '../lib/auth.js';
|
|
|
+
|
|
|
+function routeFactory({ dbs, yup, logger }) {
|
|
|
return {
|
|
|
verb: 'post',
|
|
|
path: '/goodbye',
|
|
|
handlers: [
|
|
|
+ authMiddleware,
|
|
|
function handler(req, res, next) {
|
|
|
- const contentPath = ['visits', req.body.name];
|
|
|
- dbs.visits.get(contentPath, reply => {
|
|
|
+ const contentPath = ['visits', req.user.name];
|
|
|
+ dbs.visits.get(contentPath, (reply) => {
|
|
|
const visits = reply && reply.visits ? Number(reply.visits) + 1 : 1;
|
|
|
- log.info(`Visited by ${req.params.name}.`);
|
|
|
- res.send(`hello ${req.body.name}. You have visited ${visits} times.`);
|
|
|
- next();
|
|
|
+ logger.info(`Visited by ${req.user.name}.`);
|
|
|
+ const isAdmin = isInGroup(req.user, 'admin');
|
|
|
+ res.json({ name: req.user.name, visits, isAdmin });
|
|
|
dbs.visits.set(contentPath, { visits });
|
|
|
+ return next();
|
|
|
});
|
|
|
},
|
|
|
],
|