-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
188 lines (159 loc) · 4.5 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
import express from "express";
import path from "path";
import mongoose from "mongoose";
import cookieParser from "cookie-parser";
import jwt from "jsonwebtoken";
import bcrypt from "bcrypt";
const port = process.env.PORT || 3008;
import dotenv from "dotenv";
dotenv.config();
//passward = 3gEW0yLODDsWAoV1
//const username = "Aloktam123";
//const password = "<password>";
//const cluster = "<cluster name>";
//const dbname = "myFirstDatabase";
//const uris = `mongodb+srv://${username}:${password}@${cluster}.mongodb.net/${dbname}?retryWrites=true&w=majority`;
//const uri = "mongodb+srv://Aloktam123:QxBiftJl36u85JkT@cluster0.5vq9abp.mongodb.net/?retryWrites=true&w=majority";
const user = [];
const userData = [];
//mangodb connection
//const urise = "mongodb+srv://amwaura89:password@cluster0.uim76jv.mongodb.net/?retryWrites=true&w=majority";
const uri =
process.env.MANGO_URI ||
"mongodb+srv://Aloktam1234:3gEW0yLODDsWAoV1@cluster0.lhpfmkl.mongodb.net/?retryWrites=true&w=majority";
mongoose.set("strictQuery", false);
async function connect() {
try {
await mongoose.connect(uri);
console.log("connected to MongoDB");
} catch (error) {
console.log(error);
}
}
connect();
const userSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
});
const User = mongoose.model("User", userSchema);
const app = express();
// Using Middlewares
app.use(express.static(path.join(path.resolve(), "public")));
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// Setting up View Engine
app.set("view engine", "ejs");
const isAuthenticated = async (req, res, next) => {
const { token } = req.cookies;
if (token) {
const decoded = jwt.verify(token, "sdjasdbajsdbjasd");
req.user = await User.findById(decoded._id);
next();
} else {
res.redirect("/login");
}
};
app.get("/", isAuthenticated, (req, res) => {
res.render("logout", { name: req.user.name });
});
app.get("/login", (req, res) => {
res.render("login");
});
app.get("/register", (req, res) => {
res.render("register");
});
app.get("/prescription", (req, res) => {
res.render("prescription");
});
app.get("/schemes", (req, res) => {
res.render("scheme");
});
app.get("/appointment", (req, res) => {
res.render("appoint");
});
app.get("/home", (req, res) => {
res.render("home");
});
app.get("/appointment", (req, res) => {
res.render("appoint");
});
app.get("/chatbot", (req, res) => {
res.render("chatbot");
});
app.get("/information", (req, res) => {
res.render("Information");
});
app.post("/login", async (req, res) => {
const { email, password } = req.body;
let user = await User.findOne({ email });
if (!user) return res.redirect("/register");
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch)
return res.render("login", { email, message: "Incorrect Password" });
const token = jwt.sign({ _id: user._id }, "sdjasdbajsdbjasd");
res.cookie("token", token, {
httpOnly: true,
expires: new Date(Date.now() + 60 * 1000),
});
res.redirect("/");
});
app.post("/register", async (req, res) => {
const { name, email, password } = req.body;
let user = await User.findOne({ email });
if (user) {
return res.redirect("/login");
}
const hashedPassword = await bcrypt.hash(password, 10);
user = await User.create({
name,
email,
password: hashedPassword,
});
const token = jwt.sign({ _id: user._id }, "sdjasdbajsdbjasd");
res.cookie("token", token, {
httpOnly: true,
expires: new Date(Date.now() + 60 * 1000),
});
res.redirect("/information");
});
app.get("/logout", (req, res) => {
res.cookie("token", null, {
httpOnly: true,
expires: new Date(Date.now()),
});
res.redirect("/home");
});
app.get("/signout", (req, res) => {
res.render("logout", { name: req.body.name });
});
app.post("/appoint", (req, res) => {
user.push({
patientname: req.body.name,
patientdate: req.body.date,
patientTime: req.body.time,
patientNumber: req.body.number,
patientState: req.body.state,
patientCity: req.body.city,
patientHospital: req.body.hospital,
});
res.render("appoint");
});
app.get("/user", (req, res) => {
res.json(user);
});
app.post("/userData", (req, res) => {
userData.push({
name: req.body.name,
fatherName: req.body.fatherName,
phoneNum: req.body.phone,
BirthDate: req.body.birthDate,
});
res.redirect("/");
});
app.get("/userData", (req, res) => {
res.json(userData);
});
app.listen(port, () => {
console.log(`Server is working ${port}`);
});