-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
244 lines (228 loc) · 7.03 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const express = require("express");
const app = express();
const path = require("path");
const bodyParser = require("body-parser");
// const cookieParser = require("cookie-parser");
// const expressSession = require("express-session");
const cors = require("cors");
const _ = require("lodash");
const jwt = require("jsonwebtoken");
const morgan = require("morgan");
const glob = require("glob");
require("dotenv").config();
// const barrier = require("./server/barrier.js");
const validator = require("./server/validate.js");
const mailer = require("./server/mailer.js");
// app.use(morgan(":method :url :status"));
// database
const database = require("./server/db.js");
const utils = require("./server/utils.js");
/* middleware */
// accept cross-origin requests
app.use(cors());
// parse incoming parameter requests to req.body
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(bodyParser.json());
// allow use of cookies
// app.use(
// cookieParser("sendmancookiesss", {
// secure: true,
// })
// );
/**
* "sendmancookiesss", {
secure: true,
sameSite: "Strict"
}
*/
// track logged in user across sessions
// app.use(
// expressSession({
// key: "session_id",
// secret: "sendmansecretapp",
// resave: true,
// saveUninitialized: false,
// cookie: {
// sameSite: "strict",
// },
// })
// );
// check jwt authorization
var auth = (req, res, next) => {
const authHeader = req.headers.authorization;
res.locals.barrier = {};
res.locals.barrier.authenticated = false;
if (authHeader) {
try {
const token = authHeader.split(" ")[1];
jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
if (err) {
return res.status(401).send("Authorization failed");
}
if (typeof role === "string" && user.role != role) {
return res.status(401).send("Authorization failed");
}
res.locals.barrier.user = user;
res.locals.barrier.authenticated = true;
next();
});
} catch (error) {
console.log(`Not authorized to access : ${req.originalUrl}`);
return res.status(401).send("Authorization failed");
}
} else {
console.log(req.headers);
console.log(`Not authorized to access : ${req.originalUrl}`);
return res.status(401).send("Authorization required");
}
};
app.set("port", process.env.PORT || 8080);
app.use(express.static(__dirname + "/public"));
/* routes */
let check_in = require("./routes/checkin");
app.use("/users", auth, check_in, require("./routes/users"));
app.use("/newchat", auth, check_in, require("./routes/newchat"));
app.use("/upload", auth, check_in, require("./routes/upload"));
app.use("/transfers", auth, check_in, require("./routes/transfers"));
app.use("/download", require("./routes/download")); // no need for auth to download
app.use("/images", require("./routes/images")); // no need for auth when loading images
app.post("/resetpassword", async (req, res) => {
let { email } = req.body;
let db = database.connect("users");
let match = _.filter(db.value(), { mainEmail: email });
if (!match.length) {
return res.status(400).send("This email is not registered with sendman");
}
match = match[0];
let password = utils.randomString(15, false);
match.password = password;
mailer.send(email, "password", { password: password }).catch((err) => {});
db.set(match.id + "." + password).write();
res.json({ done: true });
});
app.post("/signin", (req, res, next) => {
let { email, password } = req.body;
let db = database.connect("users");
let match = _.filter(db.value(), { mainEmail: email });
if (!match.length) {
return res.status(400).send("Incorrect email or password");
}
match = match[0];
if (match.password != password) {
return res.status(400).send("Incorrect email or password");
}
let id = match.id;
console.log(id);
const token = jwt.sign(
{
role: "user",
user_id: id,
authenticated: true,
},
process.env.JWT_SECRET
);
console.log(token);
res.json({ done: true, token });
console.log("Logging in complete");
// req.session.barrier.user.user_id
// res.status(400).send("testing");
// if (res.locals.barrier.authenticated) {
// const token = jwt.sign(
// {
// role: "user",
// id: user.id,
// },
// process.env.JWT_SECRET
// );
// res.json({ token });
// res.status(200).json({ done: true });
// } else {
// let error = res.locals.barrier.error;
// res.status(500).send(error);
// }
});
app.post(
"/signup",
async (req, res, next) => {
let { username, number, password, email } = req.body;
console.log(username, password, email);
let validate = validator.normal;
let validation = validate(username, password, email);
if (validation.length) {
res.status(400).send("Invalid registration details");
} else {
try {
let db = database.connect("users");
let id = utils.randomString(30);
let user = utils.createUser({
username,
number,
password,
email,
id,
});
let matches = _.filter(db.value(), { mainEmail: email });
let exists = Boolean(matches.length);
// console.log(matches);
if (exists) {
console.log(matches);
res.status(400).send("An account with this email already exists");
} else {
db.set(id, user).write();
let pathToFiles = path.resolve(
__dirname,
"./server/storage/files/" + id
);
await utils.makeDir(pathToFiles);
res.locals.user_id = id;
next();
}
} catch (error) {
if (!error) {
console.log("no error");
res.status(500).send("Error registering. Please try again.");
err = true;
} else {
console.log(error);
res.status(500).send("Error registering. Please try again.");
}
}
}
},
(req, res, next) => {
// console.log(`Authenticated? ${res.locals.barrier.authenticated}`);
let id = res.locals.user_id;
const token = jwt.sign(
{
role: "user",
user_id: id,
authenticated: true,
},
process.env.JWT_SECRET
);
// console.log(token);
res.json({ done: true, token });
// console.log("Registration complete");
}
);
/* ERROR pages */
app.use((req, res) => {
res.status(404);
res.set("Content-Type", "text/plain");
res.send("404 - Page Not Found");
}); // 404
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
// add_err_to_log(500);
res.status(500);
res.set("Content-Type", "text/plain");
res.send("500 - Internal Server Error");
}); //500
// start server
app.listen(app.get("port"), () => {
console.log("Running chat app on localhost://" + app.get("port"));
});