forked from pravega-web/pravega
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
72 lines (66 loc) · 1.5 KB
/
user.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
module.exports = (app) => {
// Create user
app.post("/api/create/user", (req, res) => {
// Add all preprocessing to data here
var newUser = req.body;
genRefId(newUser, res);
});
// Find user
app.post("/api/find/user", (req, res) => {
user.find(req.body, (err, data) => {
if (err) throw err;
res.send(data);
});
});
// Updating user
app.post("/api/update/user", (req, res) => {
console.log(req.body);
user.updateOne({ _id: req.body._id }, req.body, (err, data) => {
if (err) {
throw err;
}
console.log("Updated: ", data.nModified == 1);
res.send(data);
});
});
// pword update
app.post("/api/update/user/pword", (req, res) => {
res.send("OK");
});
// Login
app.post("/api/login", (req, res) => {
user.find(
{ email: req.body.username, pword: req.body.password },
(err, data) => {
if (err) {
throw err;
}
if (!data[0]) {
res.status(401).send("Auth failed: Wrong creds");
} else {
res.send(data);
}
}
);
});
}
function genRefId(newUser, res) {
rand = Math.floor(Math.random() * 100000);
user.findOne({ refId: rand }, (err, data) => {
if (err) {
throw err;
}
//Check unique
if (data) {
genRefId(newUser);
} else {
newUser.refId = rand;
user.create(newUser, (err1, data) => {
if (err1) {
throw err1;
}
res.send(data);
});
}
});
}