This commit is contained in:
les
2019-09-11 19:12:24 +02:00
parent 93baf01a55
commit 2fe956d117
65 changed files with 762 additions and 721 deletions

View File

@@ -14,6 +14,8 @@ const settingsController = require('./controller/settings')
const storage = require('./storage')
const upload = multer({ storage })
const debug = require('debug')('api')
const api = express.Router()
api.use(cookieParser())
api.use(bodyParser.urlencoded({ extended: false }))
@@ -24,10 +26,10 @@ const jwt = expressJwt({
credentialsRequired: false,
getToken: function fromHeaderOrQuerystring (req) {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
return req.headers.authorization.split(' ')[1];
return req.headers.authorization.split(' ')[1]
} else if (req.cookies && req.cookies['auth._token.local']) {
const [ prefix, token ] = req.cookies['auth._token.local'].split(' ')
if (prefix === 'Bearer') return token
if (prefix === 'Bearer') { return token }
}
}
})
@@ -47,10 +49,10 @@ api.post('/user', jwt, isAuth, isAdmin, userController.create)
// update user
api.put('/user', jwt, isAuth, userController.update)
//delete user
// delete user
api.delete('/user/:id', jwt, isAuth, isAdmin, userController.remove)
//
//
// api.delete('/user', userController.remove)
// get all users
@@ -64,7 +66,7 @@ api.put('/place', jwt, isAuth, isAdmin, eventController.updatePlace)
// add event
api.post('/user/event', jwt, fillUser, upload.single('image'), userController.addEvent)
// update event
api.put('/user/event', jwt, isAuth, upload.single('image'), userController.updateEvent)
@@ -100,4 +102,16 @@ api.get('/export/:type', exportController.export)
api.get('/event/:month/:year', eventController.getAll)
// Handle 404
api.use((req, res) => {
debug('404 Page not found: %s', req.path)
res.status(404).send('404: Page not Found')
})
// Handle 500
api.use((error, req, res, next) => {
debug(error)
res.status(500).send('500: Internal Server Error')
})
module.exports = api