-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
52 lines (46 loc) · 1.47 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require('./db/mongoose').resolve
require('dotenv').config()
const CommonResponse = require('./db/models/common-models/commonResponse')
const jwt = require('jsonwebtoken');
// HTTP SERVER
const express = require('express')
const PORT = process.env.PORT || 3000
const app = express()
// MIDDLEWARES <- THOSE ARE RUNNING ON EVERY REQUEST
const bodyParser = require('body-parser')
app.use(bodyParser.json({limit: '5mb'}))
app.use(bodyParser.urlencoded({limit: '5mb',extended: true}))
const auth = (req, res, next) => {
const token = req.header('auth-token');
if (!token) {
error = new CommonResponse({
success: false,
message: 'Authorization failed'
})
return res.json(error);
}
try {
const verified = jwt.verify(token, process.env.TOKEN_SECRET);
req.user = verified;
next(); // to continue the flow
} catch (err) {
let error = new CommonResponse({
success: false,
message: 'Token is not valid'
})
res.json(error);
}
};
// ROUTES
const projectsRoute = require('./routes/projects')
const tasksRoute = require('./routes/tasks')
const usersRoute = require('./routes/users')
const worklogerRoute = require('./routes/workloger')
const authenticationRoute = require('./routes/authentication')
app.use('/authentication', authenticationRoute)
app.use('/users', auth, usersRoute)
app.use('/projects', auth, projectsRoute)
app.use('/tasks', auth, tasksRoute)
app.use('/workloger', auth, worklogerRoute)
app.listen(PORT)
module.exports = app;