-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
52 lines (40 loc) · 1.47 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require("path");
const db = require('./db/database');
require('./auth');
const userRouter = require('./routes/user-router');
const gardener = require('./routes/gardener-route');
const manager = require('./routes/manager-route');
const passport = require('passport');
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use(express.json());
app.use(bodyParser.json());
db.on('error', console.error.bind(console, 'Mongodb connection error:'));
//Checking role
async function managerAuth(req, res, next) {
if (req.user && req.user.role === 'manager') {
next();
} else {
return res.status(401).json({
message: 'Unauthorized, not a manager'
});
}
}
app.use('/all', userRouter);
//adding authentication with passport
app.use('/gardener', passport.authenticate('jwt', { session: false }), gardener);
app.use('/manager', passport.authenticate('jwt', { session: false }), managerAuth, manager);
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({ error: err });
});
app.use(express.static(path.join(__dirname, "client", "build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
app.listen(port, () => console.log(`Server is up and running on port ${port}`));