-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
148 lines (131 loc) · 3.46 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
const express = require("express");
const ejs = require("ejs");
const app = express();
require("dotenv").config();
const bodyParser = require("body-parser");
const bcrypt = require("bcrypt");
const mongoose = require("mongoose");
const session = require("express-session");
const MongoDBSession = require("connect-mongodb-session")(session);
const popup = require("node-popup");
const mongoURI = process.env.MONGODB_URI;
mongoose
.connect(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.catch((e) => console.log(e));
const store = new MongoDBSession({
uri: mongoURI,
collection: "currentSessions",
});
app.set("view-engine", "ejs");
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(express.static("./public"));
app.use(
session({
secret: "secret",
resave: false,
saveUninitialized: false,
store: store,
})
);
const todoSchema = new mongoose.Schema({
username: {
type: String,
index: true,
},
password: String,
todos: [],
});
const users = new mongoose.model("users", todoSchema);
app.get("/", (req, res) => {
res.redirect("/login");
});
app.get("/login", (req, res) => {
res.render("../public/login.ejs", { msg: "" });
});
app.get("/todos", async (req, res) => {
console.log(req.session.usern);
const user = await users.findOne({ username: req.session.usern });
if (req.session.isAuth) {
res.render("../public/todo.ejs", {
values: user.todos,
name: user.username,
});
} else {
res.redirect("/login");
}
});
app.post("/login", async (req, res) => {
var userData = await users.findOne({ username: req.body.username });
if (!userData) {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
var user = new users({
username: req.body.username,
password: hashedPassword,
todos: [],
});
user.save();
req.session.usern = user.username;
req.session.isAuth = true;
return res.redirect("/todos");
} else if (
userData.username == req.body.username &&
(await bcrypt.compare(req.body.password, userData.password))
) {
req.session.usern = userData.username;
req.session.isAuth = true;
return res.redirect("/todos");
} else {
res.render("../public/login.ejs", { msg: "Username already exists" });
}
});
app.post("/addtask", async (req, res) => {
if (!req.body.task) {
return res.redirect("/todos");
}
if (req.session.isAuth) {
newtask = req.body.task;
var userData = await users.findOne({ username: req.body.name });
var newtodos = userData.todos;
if (newtodos.indexOf(newtask) == -1) {
newtodos.push(newtask);
}
await users.updateOne(
{ username: req.body.name },
{ $set: { todos: newtodos } }
);
}
res.redirect("/todos");
});
app.post("/deletetask", async (req, res) => {
if (req.session.isAuth) {
let user = await users.findOne({ username: req.body.name });
old_todos = user.todos;
var updated_todos = old_todos.filter((todo) => {
return todo != req.body.value;
});
await users.updateOne(
{ username: req.body.name },
{ $set: { todos: updated_todos } }
);
}
// res.render('../public/todo.ejs',{name:user.username,values:updated_todos})
res.redirect("/todos");
});
app.post("/logout", (req, res) => {
req.session.destroy();
res.redirect("/");
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 1100;
}
app.listen(port, async () => {
console.log("Server started");
});