-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
109 lines (98 loc) · 2.8 KB
/
routes.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
const express = require("express");
const dbCalls = require("./db/main");
const passport = require("passport");
const app = express();
const connectEnsureLogin = require("connect-ensure-login");
const User = require("./db/schemas/user");
const httpStatusCodes = require("http-status-codes");
const { StatusCodes } = httpStatusCodes;
app.get("/", connectEnsureLogin.ensureLoggedIn("/signin"), async (req, res) => {
let reminders = [];
try {
reminders = await dbCalls.getReminders(req.user.email);
} catch (e) {}
res.render("main", {
layout: "index",
reminders: reminders,
minDate: new Date().toISOString().split("T")[0], // minimum date that can be selected
});
});
app.post(
"/save-reminder",
connectEnsureLogin.ensureLoggedIn("/signin"),
async (req, res) => {
try {
const reminder = await dbCalls.saveReminder({
...req.body,
createdBy: req.user.email,
});
res
.status(StatusCodes.OK)
.render("thank-you", { layout: "index", data: reminder, saved: true });
} catch (e) {
res.status(StatusCodes.INTERNAL_SERVER_ERROR).send(e);
}
}
);
app.get("/reminders", async (req, res) => {
try {
const reminders = await dbCalls.getReminders(req.user.email);
res.status(StatusCodes.OK).send(reminders);
} catch (e) {
res.status(StatusCodes.INTERNAL_SERVER_ERROR).send(e);
}
});
app.post("/delete-reminder", async (req, res) => {
const reminderId = req.body.id;
try {
await dbCalls.deleteReminder(reminderId);
res
.status(StatusCodes.OK)
.render("thank-you", { layout: "index", id: reminderId, deleted: true });
} catch (e) {
res.status(StatusCodes.INTERNAL_SERVER_ERROR).send(e);
}
});
app.get("/signup", (req, res) => {
res.render("signup", { layout: "index" });
});
app.get("/signin", (req, res) => {
if (req.user) {
res.redirect("/");
} else {
res.render("login", { layout: "index" });
}
});
app.post("/register", async (req, res) => {
try {
User.register(
{ email: req.body.email, name: req.body.name, _id: req.body.email },
req.body.password,
async function (err, user) {
if (err) {
console.log(err.name, "error");
if (err.name === "UserExistsError") {
res.status(StatusCodes.CONFLICT).send(err.message);
}
}
if (user) {
await User.authenticate()(req.body.email, req.body.password);
if (reponse.user) {
res.redirect("/");
}
}
}
);
} catch (e) {
res.status(StatusCodes.INTERNAL_SERVER_ERROR).send(e);
}
});
app.post("/login", passport.authenticate("local"), function (req, res) {
res.redirect("/");
});
app.post("/logout", (req, res) => {
req.logout(function (err, data) {
res.redirect("/signin");
});
});
module.exports = app;