-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
150 lines (130 loc) · 3.98 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
144
145
146
147
148
149
150
const express = require("express");
const cors = require("cors");
const bcrypt = require("bcryptjs");
const nunjucks = require("nunjucks");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const MongoStore = require("connect-mongo");
const mongoose = require("mongoose");
const { Patient, Physician } = require("./db");
require("dotenv").config();
const app = express();
nunjucks.configure("views", {
autoescape: true,
express: app,
});
mongoose.connect(
"mongodb+srv://MedAiAdmin:admin@medaicluster.ruzyfzy.mongodb.net/medai?retryWrites=true&w=majority&appName=MedAiCluster",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
app.use(
session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl:
"mongodb+srv://MedAiAdmin:admin@medaicluster.ruzyfzy.mongodb.net/medai?retryWrites=true&w=majority&appName=MedAiCluster",
}),
cookie: {
maxAge: 1000 * 60 * 60 * 24,
},
})
);
app.use(cors());
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));
const PORT = 3000;
function isAuthenticated(req, res, next) {
if (req.session.user) {
return next();
} else {
res.redirect("/");
}
}
app.get("/", async function (req, res) {
res.render("login.njk");
});
app.post("/", async function (req, res) {
console.log("we are logging in!!!");
console.log("body ", req.body);
if (!req.body.username || !req.body.password) {
res.render("login.njk", {
error_message: "Missing username and/or password",
});
console.log("Missing username and/or password");
return;
}
const physician = await Physician.findOne({ Username: req.body.username });
if (!physician) {
res.render("login.njk", { error_message: "Bad username" });
console.log("Bad username");
} else {
if (bcrypt.compareSync(req.body.password, physician.Password)) {
req.session.user = physician;
if (req.body.rememberMe) {
req.session.cookie.maxAge = 1000 * 60 * 60 * 24 * 30;
} else {
req.session.cookie.expires = false;
}
res.redirect("/dashboard");
} else {
res.render("login.njk", { error_message: "Incorrect password" });
console.log("Incorrect password");
}
}
});
app.get("/dashboard", isAuthenticated, async function (req, res) {
try {
const patient = await Patient.findOne();
if (!patient) {
return res.status(404).send("No patients found");
}
const savedPatientID = req.cookies.savedPatientID;
if (!savedPatientID) {
res.cookie("savedPatientID", patient.patient_id);
}
res.redirect(`/dashboard/${savedPatientID || patient.patient_id}`);
} catch (error) {
console.error("Error fetching patients:", error);
res.status(500).send("Internal Server Error");
}
});
app.get("/dashboard/:patientId", isAuthenticated, async function (req, res) {
const patientId = req.params.patientId;
try {
const patient = await Patient.findOne({ patient_id: patientId });
if (!patient) {
return res.status(404).send("Patient not found");
}
const patients = await Patient.find();
res.render("dashboard.njk", { patient: patient, patients: patients });
} catch (error) {
console.error("Error fetching patient:", error);
res.status(500).send("Internal Server Error");
}
});
app.get("/settings", isAuthenticated, async function (req, res) {
res.render("settings.njk");
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
app.get("/patients/:id", async function (req, res) {
const patientId = req.params.id;
try {
const patient = await Patient.findOne({ patient_id: patientId });
if (!patient) {
return res.status(404).send("Patient not found");
}
res.json(patient);
} catch (error) {
console.error("Error fetching patient:", error);
res.status(500).send("Internal Server Error");
}
});