-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (68 loc) · 2.1 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const express = require("express");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const productRouter = require("./src/routes/products/products.js");
const categoriesRouter = require("./src/routes/categories/categories.js");
const { signupRouter } = require("./src/routes/user/signin.js");
const { loginRouter } = require("./src/routes/user/login.js");
const { logoutRouter } = require("./src/routes/user/logout.js");
const ordersRouter = require("./src/routes/orders/orders.js");
const { userUpdateRouter } = require("./src/routes/user/updateUser.js");
const mongoose = require("mongoose");
require("dotenv/config");
const Users = require("./src/database/schemas/userSchema.js");
const jwt = require("jsonwebtoken");
const { cartRouter } = require("./src/routes/cart/cart.js");
const app = express();
app.use(
cors({
origin: [
"https://react-shop-front-siza.liara.run",
"http://localhost:5173",
"https://react-shop-siza.vercel.app",
],
methods: ["GET", "POST", "PUT"],
credentials: true,
})
);
app.use(express.json());
app.use(cookieParser());
app.use(productRouter);
app.use(categoriesRouter);
app.use(signupRouter);
app.use(loginRouter);
app.use(logoutRouter);
app.use(userUpdateRouter);
app.use(cartRouter);
app.use(ordersRouter);
mongoose
.connect(process.env.DB_URI)
.then(() => console.log(`Connected to DB`))
.catch((err) => console.log(err));
app.get("/", (req, res) => {
const token = req.cookies.token;
if (!token) {
return res.json({ status: false });
}
jwt.verify(token, process.env.TOKEN_KEY, async (err, data) => {
if (err) {
return res.json({ status: false });
} else {
const user = await Users.findById(data.id);
if (user)
return res.json({
status: true,
userInfo: {
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
_id: user._id,
},
});
else return res.json({ status: false });
}
});
});
app.listen(process.env.PORT, () =>
console.log(`listening on port : ${process.env.PORT}!`)
);