-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
43 lines (34 loc) · 1.3 KB
/
server.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
const express = require("express");
const app = express();
const productRoutes = require("./src/routes");
const cors = require("cors");
const controller = require("./src/controller");
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken')
const port = 3001;
app.use(cors());
app.use(express.json());
app.get("/", (req, res) => {
res.send("hello world");
});
function authenticateToken(req, res, next){
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[0];
console.log("user wants to come but token is " + authHeader);
if(token == null) return res.sendStatus(401)
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, user) =>
{
if(err) return res.sendStatus(403);
req.user = user;
next();
});
}
app.use("/products", productRoutes);
app.post("/login", controller.loginUser);
app.post("/createAccount", controller.createAccount);
app.get("/search/:searchQ", controller.getProductSearch);
app.get("/cart", authenticateToken, controller.getCart);
app.post("/report", authenticateToken, controller.createReport);
app.post('/addToCart', authenticateToken, controller.addToCart);
app.delete('/deleteFromCart', authenticateToken, controller.deleteFromCart);
app.listen(port, () => console.log(`app listening on port ${port}`));