2020-03-25 02:54:31 +00:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const Authenticator = require('../Authenticator');
|
2020-03-25 18:27:58 +00:00
|
|
|
const UserController = require('../controllers/UserController');
|
|
|
|
const PublicStateController = require('../controllers/PublicStateController');
|
2020-03-25 02:54:31 +00:00
|
|
|
|
|
|
|
router.get('/', function (req, res, next) {
|
|
|
|
let rnd = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
|
|
|
|
res.send('respond with some api calls - ' + rnd);
|
|
|
|
});
|
|
|
|
|
2020-03-25 18:27:58 +00:00
|
|
|
router.post('/state', (req, res, next) => {
|
|
|
|
app = req.app;
|
|
|
|
db = app.locals.database;
|
|
|
|
|
|
|
|
let authenticator = new Authenticator(db);
|
|
|
|
let ctrl = new PublicStateController(db);
|
|
|
|
|
|
|
|
let scb = (data) => {
|
|
|
|
res.json(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
let ecb = (error) => {
|
|
|
|
res.status(400).send(err);
|
|
|
|
};
|
|
|
|
|
|
|
|
authenticator.getAuthenticatedUser(req, (user) => {
|
|
|
|
if (user) {
|
|
|
|
ctrl.create(req.body, scb, ecb);
|
|
|
|
} else {
|
|
|
|
ecb({ code: 'auth error', message: 'user not logged in.' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-25 02:54:31 +00:00
|
|
|
router.get('/user', function (req, res, next) {
|
|
|
|
app = req.app;
|
|
|
|
db = app.locals.database;
|
|
|
|
|
|
|
|
let authenticator = new Authenticator(db);
|
|
|
|
let ctrl = new UserController(db);
|
|
|
|
|
|
|
|
let scb = function (data) {
|
|
|
|
res.json(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
let ecb = function (err) {
|
|
|
|
res.status(400).send(err);
|
|
|
|
};
|
|
|
|
|
|
|
|
authenticator.getAuthenticatedUser(req, function (user) {
|
|
|
|
if (user) {
|
|
|
|
ctrl.getUserData(user, scb, ecb);
|
|
|
|
} else {
|
2020-03-25 18:27:58 +00:00
|
|
|
ecb({ code: 'auth error', message: 'user not logged in.' });
|
2020-03-25 02:54:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|