-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (101 loc) · 2.87 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require("dotenv").config();
const path = require("path");
const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const cors = require("cors");
const mongoose = require("mongoose");
const jwt = require("jsonwebtoken");
const cookieParser = require("cookie-parser");
const User = require("./models/UserModel");
const routes = require("./routes/routes");
const app = express();
// setting up the database connection and server
const startServer = async () => {
await mongoose
.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => {
console.log("Connected to MongoDB!");
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
})
.catch((error) => {
console.log(error);
});
};
// middlewares
app.use(morgan("dev"));
app.use(bodyParser.json());
app.use(cookieParser());
// cors
if (process.env.NODE_ENV === "development") {
app.use(cors({ origin: `${process.env.CLIENT_URL}` }));
} else {
app.use(cors({ origin: `${process.env.HOSTED_URL}` }));
}
const publicPath = path.join(__dirname, "build");
app.use(express.static(publicPath));
app.use("/static", express.static(path.join(__dirname, "build/static")));
app.use(
"/manifest.json",
express.static(path.join(__dirname, "build", "manifest.json"))
);
app.get("*", (req, res) => {
res.sendFile(path.join(publicPath, "index.html"));
});
app.use(async (req, res, next) => {
if (req.headers["x-access-token"]) {
const accessToken = req.headers["x-access-token"];
const { userId, exp } = await jwt.verify(
accessToken,
process.env.JWT_SECRET
);
// check if token has expired
if (exp < Date.now().valueOf() / 1000) {
return res.status(401).json({
error: "JWT token has expired. Login required to get a new one",
});
}
res.locals.loggedInUser = await User.findById(userId);
next();
} else {
next();
}
});
app.use("/api/v1", routes);
app.post("/api/v1/store-token", (req, res) => {
const { token } = req.body;
if (token && token !== "") {
res
.cookie("accessToken", token, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
})
.status(200)
.json({ message: "Logged in successfully" });
} else {
res.status(404).json({ error: "No token obtained" });
}
});
app.post("/api/v1/get-token", (req, res) => {
const token = req.cookies.accessToken;
console.log(token);
if (!token) {
res
.status(403)
.json({ error: "Unauthorized" });
}
res.status(200).json({ token: token });
});
app.post("/api/v1/logout", (req, res) => {
return res
.clearCookie("accessToken")
.status(200)
.json({ message: "Successfully logged out, Please come back soon." });
});
const PORT = process.env.PORT || 3001;
startServer();