-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (124 loc) · 3.27 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
const express = require("express");
const mongoose = require("mongoose");
const session = require("express-session");
const flash = require("connect-flash");
var MongoDBStore = require("connect-mongodb-session")(session);
const http = require("http");
const User = require("./models/user");
const Pictures = require("./models/pictures")
const app = express();
const server = http.createServer(app);
const socketIO = require("socket.io");
const io = new socketIO.Server(server);
io.on("connection", (socket) => {
console.log("User connected");
socket.on("disconnect", () => {
console.log("user disconnected");
});
socket.on("chat-sent", (data) => {
console.log(data);
socket.emit("sent-response");
socket.broadcast.emit("update-chat", data);
});
});
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
app.use(express.json({}));
app.use(flash());
// uge5G5s4am7plr5q
// application
// const MONGO_URI = "mongodb://localhost:27017/ClubUser";
const MONGO_URI =
"mongodb+srv://application:[email protected]/?retryWrites=true&w=majority";
var store = new MongoDBStore({
uri: MONGO_URI,
collection: "sessions",
});
const setLocale = (req, res, next) => {
res.locals.isLogged = req.session.isLogged || false;
next();
};
const isAuth = (req, res, next) => {
if (req.session.isLogged) {
next();
}
res.status(302).redirect("/login");
};
app.use(
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: true,
store: store,
})
);
mongoose
.connect(MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
server.listen("3000", () => {
console.log("Listening to port 3000");
});
})
.catch((err) => {
console.log("OH NO error");
});
app.use(setLocale);
app.get("/", (req, res) => {
res.render("home");
});
app.get("/profile", (req, res) => {
res.render("profile", { user: req.session.user || "Guest" });
});
app.get("/gallery", (req, res) => {
res.render("gallery");
});
app.get("/login", (req, res) => {
res.render("login");
});
app.get("/signup", (req, res) => {
res.render("signup");
});
app.post("/signup", async (req, res) => {
const { password, cpassword, firstname, lastname, email } = req.body;
User.findOne({ email: email }).then(async (user) => {
if (user) {
res.send("User already exists");
}
// if (password != cpassword) {
// req.flash("message", "Confirm Password does not match!");
// return res.redirect("/signup");
// }
const epasswd = password;
const newUser = new User({
firstname,
lastname,
password: epasswd,
email,
});
await newUser.save();
req.session.isLogged = true;
res.redirect("/login");
});
});
app.post("/login", async (req, res) => {
var email = req.body.email;
var password = req.body.password;
const database = await User.findOne({ email: email });
if (!database) {
res.send("NO USER FOUND");
} else if (database.password != password) {
res.send("INCORRECT PASSWORD");
} else {
req.session.isLogged = true;
req.session.user = database;
res.redirect("profile");
}
});
app.get("/logout", (req, res) => {
req.session.destroy();
res.redirect("/");
});