-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
225 lines (194 loc) · 5.55 KB
/
app.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
const express = require("express");
// For when we create our apis
const cors = require("cors");
const mongoose = require("mongoose");
const session = require("express-session");
// For session Storage
const MongoStore = require("connect-mongo")(session);
const config = require("./config/database");
let configuration = process.env.DATABASE || config.database;
const Links = require("./models/links");
const User = require("./models/users");
var jwt = require("jsonwebtoken");
let secret = process.env.SECRET || config.secret;
// connect to database
mongoose.connect(configuration, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", function () {
console.log("mongodb connection established");
});
// initialize app
const app = express();
// middlewares
app.use(
session({
secret: process.env.SECRET || config.secret,
store: new MongoStore({ mongooseConnection: db }),
resave: false,
saveUninitialized: false,
})
);
app.use(cors());
app.use(express.static(__dirname + "/views"));
app.use(express.json({ limit: "10mb" }));
// Auth (User) Routes
const userRoutes = require("./routes/userRoutes");
app.use("/api", userRoutes);
const dashboard = require("./routes/dashboard");
app.use("/api", dashboard);
const manage = require("./routes/manage");
app.use("/api", manage);
const createCert = require("./routes/createCert");
app.use("/api", createCert);
const addnew = require("./routes/addEligibleUsers");
app.use("/api", addnew)
const generate = require("./routes/generate");
app.use("/api", generate);
app.get('/signup', (req, res) => {
res.sendFile(__dirname + "/views/signup.html");
});
app.get("/dashboard", (req, res) => {
if (!req.session.email && !req.session.anon) {
return res.redirect("/login");
}
res.sendFile(__dirname + "/views/dashboard.html");
})
app.get("/login", (req, res) => {
res.sendFile(__dirname + "/views/login.html");
})
app.get("/manage", (req, res) => {
if (!req.session.email && !req.session.anon) {
return res.redirect("/login");
} else {
if (req.query.link) {
Links.findOne({ link: req.query.link }, function (err, cert) {
if (cert) {
if (cert.issuer == req.session.email) {
res.sendFile(__dirname + "/views/manage.html");
} else {
res.status(401)
res.redirect("/login");
}
} else {
res.sendFile(__dirname + "/views/404-page.html");
}
})
} else {
res.sendFile(__dirname + "/views/404-page.html");
}
}
})
app.get("/createcertificate", (req, res) => {
if (!req.session.email && !req.session.anon) {
return res.redirect("/login");
}
res.sendFile(__dirname + "/views/certificate-gen.html");
})
app.get("/certificate/:link", (req, res) => {
let link = req.params.link
req.session.link = link;
Links.findOne({ link: req.params.link }, (err, cert) => {
if (cert) {
res.status(200);
return res.sendFile(__dirname + "/views/emailverify.html");
} else {
res.sendFile(__dirname + "/views/404-page.html");
}
})
})
app.get("/certify/:jwt", (req, res) => {
let token = req.params.jwt
jwt.verify(token, secret, function (err, data) {
if (err) {
res.sendFile(__dirname + "/views/404-page.html");
} else {
req.session.generator = data.email;
req.session.link = data.link;
res.sendFile(__dirname + "/views/certificator.html")
}
});
})
app.get("/verify/:jwt", (req, res) => {
let token = req.params.jwt
jwt.verify(token, secret, function (err, data) {
if (err) {
res.sendFile(__dirname + "/views/404-page.html");
} else {
User.findOne({ email: data.email }, function (err, user) {
if (user) {
user.confirmed = true;
user.save((err) => {
if (err) console.log(err)
else {
req.session.email = data.email;
res.redirect("/dashboard")
}
})
} else {
res.sendFile(__dirname + "/views/404-page.html");
}
})
}
});
})
app.get('/logout', (req, res) => {
if (req.session) {
req.session.destroy(err => {
if (err) {
res.status(400)
res.json({
status: false,
message: "unable to logout"
})
} else {
if (req.accepts('html')) {
res.redirect("/login")
} else if (req.accepts('json')) {
res.json({
status: true,
message: "logout successful"
})
}
}
});
} else {
res.redirect("/login")
}
})
app.get('/emailrecovery',(req,res)=>{
res.sendFile(__dirname+ "/views/forgot-email.html")
})
app.get('/passwordupdate',(req,res)=>{
if(!req.query.jwt){
return res.sendFile(__dirname + "/views/404-page.html");
}
let token=req.query.jwt;
jwt.verify(token, secret, function (err, data) {
if(err) return res.sendFile(__dirname + "/views/404-page.html");
return res.sendFile(__dirname+ "/views/password-reset.html")
})
})
app.get('*', (req, res) => {
res.status(404);
if (req.accepts('html')) {
return res.sendFile(__dirname + "/views/404-page.html");
} else if (req.accepts('json')) {
res.send({
status: false,
message: "endpoin not found"
});
return;
}
});
const PORT = process.env.PORT || 3333;
let server=app.listen(PORT, () => {
console.log(`server running on port ${PORT}`);
});
server.on('clientError', (err, socket) => {
console.error(err);
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});