-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
114 lines (93 loc) · 2.79 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
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
const express = require("express");
const sendOtpEmail = require("./sendOtpEmail");
const fs = require("fs");
const app = express();
app.use(express.static("public"));
app.use(express.json());
// update user ip address
app.get("/update/:token", (req, res) => {
// get token
const { token } = req.params;
if (!token) {
return res.status(400).json({ message: "Invalid token", success: false });
}
// get ip address
const ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
const port = req.socket.remotePort;
console.log({
ip,
port,
});
const users = JSON.parse(fs.readFileSync("./data.json", "utf8"));
const user = users.find((user) => user.token === token);
if (!user) {
return res.status(400).json({ message: "Invalid token", success: false });
}
// update user
if (user.ip != ip || user.port != port) {
user.ip = ip;
user.port = port;
fs.writeFileSync("./data.json", JSON.stringify(users));
res
.status(200)
.json({ message: "User updated successfully", success: true });
} else {
res.status(200).json({ message: "User already updated", success: true });
}
});
app.all("/get/:userId", (req, res) => {
const { userId } = req.params;
const users = JSON.parse(fs.readFileSync("./data.json", "utf8"));
const user = users.find((user) => user.id === userId);
if (!user) {
return res.status(400).json({ message: "User not found", success: false });
}
const userAddress = `http://${user.ip}${
user.port ? `:${Number(user.port)}` : ""
}}`;
console.log({
userAddress,
});
// forward request to user ip address
res.redirect(userAddress);
});
app.post("/register", async (req, res) => {
const { email } = req.body;
console.log({
email,
});
try {
await sendOtpEmail(email);
} catch (e) {
console.log(e);
return res.status(400).json({ message: e.message, success: false });
}
res.status(200).json({ message: "OTP sent successfully", success: true });
});
app.post("/verify", (req, res) => {
const { email, otp } = req.body;
const users = JSON.parse(fs.readFileSync("./data.json", "utf8"));
const user = users.find((user) => user.email === email);
if (!user) {
return res.status(400).json({ message: "User not found", success: false });
}
if (user.otp != otp) {
return res.status(400).json({ message: "Invalid OTP", success: false });
}
res.status(200).json({
message: "OTP verified successfully",
success: true,
data: {
privateToken: user.token,
publicToken: user.id,
},
});
});
const port = process.env.PORT || 3000;
// check if file ./data.json exists
if (!fs.existsSync("./data.json")) {
fs.writeFileSync("./data.json", JSON.stringify([]));
}
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});