-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
143 lines (132 loc) · 4.02 KB
/
server.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
const express = require("express");
const bodyParser = require("body-parser");
const low = require("lowdb");
const FileAsync = require("lowdb/adapters/FileAsync");
const {
validateUsername,
validateEmail,
validatePassword,
} = require("./validators");
// Create server
const app = express();
app.use(bodyParser.json());
// Create database instance and start server
const adapter = new FileAsync("db.json");
low(adapter)
.then((db) => {
// Routes
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.post("/register", async (req, res) => {
const username = req.body.username;
const email = req.body.email;
const familyDoctorName = req.body.familyDoctorName;
const familyDoctorEmail = req.body.familyDoctorEmail;
const phone = req.body.phone;
const emergencyPhone = req.body.emergencyPhone;
const password = req.body.password;
const role = req.body.role;
if (!validateUsername(username)) {
res.status(400).send({ error: "invalid username" });
} else if (!validateEmail(email)) {
res.status(400).send({ error: "invalid email" });
} else if (!validatePassword(password)) {
res.status(400).send({ error: "invalid password" });
} else {
if (
await db.get("users").find({ username: req.body.username }).value()
) {
res.status(400).send({ error: "username already exists" });
} else if (
await db.get("users").find({ email: req.body.email }).value()
) {
res
.status(400)
.send({ error: "email is already linked to an account" });
} else {
db.get("users")
.push({
username,
email,
familyDoctorName,
familyDoctorEmail,
phone,
emergencyPhone,
password,
role,
})
.last()
.assign({ id: Date.now().toString() })
.write()
.then((user) => res.send({ token: user.id, role: user.role }));
}
}
});
app.post("/login", async (req, res) => {
const usernameOrEmail = req.body.usernameOrEmail;
const password = req.body.password;
let user = await db
.get("users")
.find(
(u) => u.username === usernameOrEmail || u.email === usernameOrEmail
)
.value();
if (user) {
if (user.password === password) {
res.send({ token: user.id, role: user.role });
} else {
res.status(400).send({ error: "wrong password" });
}
} else {
res.status(400).send({ error: "user not found" });
}
});
app.get("/me", async (req, res) => {
const token = req.query.token;
let user = await db.get("users").find({ id: token }).value();
if (!user) {
res.status(400).send({ error: "invalid token" });
} else {
res.send({
id: user.id,
username: user.username,
email: user.email,
familyDoctorName: user.familyDoctorName,
familyDoctorEmail: user.familyDoctorEmail,
phone: user.phone,
emergencyPhone: user.emergencyPhone,
role: user.role,
});
}
});
// Set db default values
return db
.defaults({
users: [
{
id: "126451265412",
username: "bobby12",
email: "[email protected]",
familyDoctorName: "John Meyer",
familyDoctorEmail: "[email protected]",
phone: "2039481028",
emergencyPhone: "2039412478",
password: "bobby12",
role: "patient",
},
{
id: "48065478357315",
username: "tommy12",
email: "[email protected]",
phone: "2035464628",
password: "tommy12",
role: "doctor",
},
],
})
.write();
})
.then(() => {
app.listen(process.env.PORT || 3000, () => console.log("listening"));
});