-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
366 lines (347 loc) · 9.29 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
var express = require("express");
var bodyParser = require("body-parser");
var fast2sms = require("fast-two-sms");
require("dotenv").config();
var mongoose = require("mongoose");
const Patients = require("./modal/regisForPatients.js");
const Doctors = require("./modal/regisForDoctors.js");
const Staff = require("./modal/regisForStaff.js");
const bcrypt = require("bcryptjs");
const app = express();
app.use(bodyParser.json());
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.engine("html", require("ejs").renderFile);
app.set("view engine", "html");
app.set("views", "public");
mongoose.connect("mongodb://Localhost:27017/test11", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
var db = mongoose.connection;
db.on("error", () => console.log("Error in connecting to Mongo"));
db.once("open", () => console.log("Connected to database"));
app.post("/signup", async (req, res, cb) => {
console.log(req.body.number);
usernumberreg = await Patients.findOne({ number: req.body.number }).lean();
usernumberreg2 = await Staff.findOne({ number: req.body.number }).lean();
usernumberreg3 = await Doctors.findOne({ number: req.body.number }).lean();
if (
usernumberreg == null &&
usernumberreg2 == null &&
usernumberreg3 == null
) {
var firstName = req.body.first;
var lastName = req.body.last;
var username = req.body.username;
chcekname = req.body.username;
var password = req.body.password;
var number = req.body.number;
var userType = req.body.type;
var password = await bcrypt.hash(password, 10);
data = {
firstname: firstName,
lastname: lastName,
username: username,
password: password,
number: number,
UserType: userType
};
otp = Math.floor(1000 + Math.random() * 9000);
console.log(otp);
const response = await fast2sms.sendMessage({
authorization: process.env.API_KEY,
message: otp,
numbers: [req.body.number]
});
console.log(response);
console.log(Object.values(data)[Object.values(data).length - 1]);
// console.log(req.body.type);
return res.render("otp.html");
} else {
res.render("noreg.html", {
name: "dear user",
message: "this number is already registered",
bod: "login",
linkm: "go to login",
title: "Already registered"
});
}
});
app.post("/otpind", async (req, res) => {
var otpen = req.body.otpin;
// console.log(otpen, otp);
if (otpen == otp) {
console.log(chcekname);
if(( await Doctors.findOne({ username: chcekname }).lean()==null)&&( await Staff.findOne({ username: chcekname }).lean()==null)&& (await Patients.findOne({ username: chcekname }).lean()==null)) {
try {
if (Object.values(data)[Object.values(data).length - 1] == "Patient") {
const respon = await Patients.create(data);
console.log(`successfully registered ${respon}`);
}
else if (
Object.values(data)[Object.values(data).length - 1] == "Doctor"
) {
const respon = await Doctors.create(data);
console.log(`successfully registered ${respon}`);
} else if (
Object.values(data)[Object.values(data).length - 1] == "staff"
) {
const respon = await Staff.create(data);
console.log(`successfully registered ${respon}`);
}
} catch (e) {
// console.log(e);
if (e.code === 11000) {
console.log("username taken");
res.render("noreg.html", {
name: "dear user",
message: "the username is already taken",
bod: "regis",
linkm: "try again",
title: "taken"
});
}
throw e;
}
return res.redirect("finish.html");
}
else {
console.log("username taken");
res.render("noreg.html", {
name: "dear user",
message: "the username is already taken",
bod: "regis",
linkm: "try again",
title: "taken"
});
}
} else {
otp = Math.floor(1000 + Math.random() * 9000);
console.log("try again");
res.redirect("regis.html");
}
});
app.get("/regis", (req, res) => {
console.log("Going to regis page");
res.redirect("regis.html");
});
app.get("/login", (req, res) => {
console.log("Going to login page");
res.redirect("login.html");
});
app.post("/logind", async (req, res) => {
console.log("Going to logind page");
const loginuser = req.body.loginname;
const loginpow = req.body.loginpow;
const patientlogin = await Patients.findOne({ username: loginuser }).lean();
const doctorlogin = await Doctors.findOne({ username: loginuser }).lean();
const stafflogin = await Staff.findOne({ username: loginuser }).lean();
// console.log(userlogin);
if (patientlogin != null) {
if (await bcrypt.compare(loginpow, patientlogin.password)) {
console.log("logged in");
dctrlist=await Doctors.find().lean();
arr1=[];
dctrlist.forEach((dctr) => {
// console.log(dctr.firstname);
mainname=dctr.firstname+" "+dctr.lastname;
console.log(mainname);
arr1.push(mainname);
});
console.log(arr1);
res.render("logged.html", {
dct:arr1,
name: "",
message:
"Hi! " +
patientlogin.UserType +
" " +
loginuser +
" You are logged in",
bod: "login",
linkm: "exit",
title: "Welcome"
});
} else {
console.log("wrong password");
res.render("noreg.html", {
name: loginuser,
message: "your password is incorrect",
bod: "login",
linkm: "Try again",
title: "Wrong password"
});
}
} else if (doctorlogin != null) {
if (await bcrypt.compare(loginpow, doctorlogin.password)) {
console.log("logged in");
res.render("noreg.html", {
name: "",
message:
"Hi! " +
doctorlogin.UserType +
" " +
loginuser +
" You are logged in",
bod: "login",
linkm: "exit",
title: "Welcome"
});
}
else {
console.log("wrong password");
res.render("noreg.html", {
name: loginuser,
message: "your password is incorrect",
bod: "login",
linkm: "Try again",
title: "Wrong password"
});
}
}
else if (stafflogin != null) {
if (await bcrypt.compare(loginpow, stafflogin.password)) {
console.log("logged in");
res.render("noreg.html", {
name: "",
message:
"Hi! " +
stafflogin.UserType +
" " +
loginuser +
" You are logged in",
bod: "login",
linkm: "exit",
title: "Welcome"
});
}
else {
console.log("wrong password");
res.render("noreg.html", {
name: loginuser,
message: "your password is incorrect",
bod: "login",
linkm: "Try again",
title: "Wrong password"
});
}
}
else {
console.log("not registered");
res.render("noreg.html", {
name: loginuser,
message: "you are not registered",
bod: "regis",
linkm: "Go register yourself",
title: "Not registered"
});
}
});
app.get("/forget", async (req, res) => {
res.render("forget.html");
});
app.post("/forgetotp", async (req, res) => {
forgetnum=req.body.forgetnumber;
patientnumber = await Patients.findOne({ number: forgetnum }).lean();
doctornumber = await Doctors.findOne({ number: forgetnum }).lean();
staffnumber = await Staff.findOne({ number: forgetnum }).lean();
if (patientnumber != null) {
otp = Math.floor(1000 + Math.random() * 9000);
console.log(otp);
const response = await fast2sms.sendMessage({
authorization: process.env.API_KEY,
message: otp,
numbers: [req.body.forgetnumber]
});
console.log(response);
usernumber=patientnumber;
updatedb=Patients;
res.redirect("otpforget.html");
}
else if (doctornumber != null) {
otp = Math.floor(1000 + Math.random() * 9000);
console.log(otp);
const response = await fast2sms.sendMessage({
authorization: process.env.API_KEY,
message: otp,
numbers: [req.body.forgetnumber]
});
updatedb=Doctors;
usernumber=doctornumber;
console.log(response);
res.redirect("otpforget.html");
}
else if (staffnumber != null) {
otp = Math.floor(1000 + Math.random() * 9000);
console.log(otp);
const response = await fast2sms.sendMessage({
authorization: process.env.API_KEY,
message: otp,
numbers: [req.body.forgetnumber]
});
updatedb=Staff;
usernumber=staffnumber;
console.log(response);
res.redirect("otpforget.html");
}
else {
res.render("noreg.html", {
name: "dear user",
message: "you are not registered",
bod: "regis",
linkm: "Go register yourself",
title: "Not registered"
});
}
});
app.post("/otpforget", async (req, res) => {
if (otp == req.body.otpinforget) {
res.redirect("newpass.html");
} else {
res.render("noreg.html", {
name: " ",
message: "wrong otp",
bod: "forget",
linkm: "try again",
title: "invalid otp"
});
}
});
app.post("/newpass", async (req, res) => {
console.log(usernumber._id);
// console.log(req.body.newpass);
var newpassword = await bcrypt.hash(req.body.newpass, 10);
console.log(newpassword);
// const updatelog = await updatedb.findOne({ mumber: forgetnum }).lean();
const updatepass = async _id => {
const result = await updatedb.updateOne(
{ _id },
{
$set: {
password: newpassword
}
}
);
console.log("updated successfully");
};
updatepass(usernumber._id);
res.render("noreg.html", {
name: "dear "+ usernumber.username,
message: " your password is updated successfully",
bod: "login",
linkm: "Go to login",
title: "password updated"
});
});
app
.get("/", (req, res) => {
res.set({
"Allow-access-Allow-Origin": "*"
});
return res.redirect("login.html");
})
.listen(8080, "localhost");
console.log("listening");
// updated