This repository has been archived by the owner on Apr 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
159 lines (150 loc) · 3.57 KB
/
server.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
const express = require("express");
const app = express();
const bodyparser = require("body-parser");
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/drict", {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true,
});
const message = require("./models/messages");
const user = require("./models/users");
const passport = require("passport");
const passport_local = require("passport-local");
const passport_local_mongoose = require("passport-local-mongoose");
app.use(
require("express-session")({
secret: "random secret => have no clue what does",
resave: false,
saveUninitialized: false,
})
);
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(bodyparser.urlencoded({ extended: true }));
app.use(bodyparser.json());
app.use(passport.initialize());
app.use(passport.session());
passport.use(new passport_local(user.authenticate()));
passport.serializeUser(user.serializeUser());
passport.deserializeUser(user.deserializeUser());
app.use((req, res, next) => {
res.locals.currentUser = req.user;
next();
});
//================
//ROUTES
//================
app.get("/", (req, res) => {
message.find({}, (err, msg) => {
if (err) throw err;
res.render("index", { msg: msg });
});
});
app.post("/", (req, res) => {
if (!res.locals.currentUser) {
message.create({
name: "anonim",
text: req.body.text,
});
} else {
message.create({
name: res.locals.currentUser.username,
text: req.body.text,
});
}
res.redirect("/");
});
//REGISTER routes
app.get("/signup", (req, res) => {
res.render("signup");
});
app.post("/signup", (req, res) => {
user.register(
new user({
username: req.body.username,
avatar: req.body.avatar,
}),
req.body.password,
(err, user) => {
if (err) {
console.log(err);
return res.render("signup");
}
passport.authenticate("local")(req, res, () => {
res.redirect("/");
});
}
);
});
//LOGIN routes
app.get("/login", (req, res) => {
res.render("login");
});
app.post(
"/login",
passport.authenticate("local", {
successRedirect: "/",
failureRedirect: "/login",
}),
(req, res) => {}
);
//LOGOUT routes
app.get("/logout", (req, res) => {
req.logout();
res.redirect("/");
});
// AJAX requests test
app.get("/ajax", (req, res) => {
message.find({}, (err, msg) => {
if (err) throw err;
res.send(JSON.stringify(msg));
});
});
app.post("/ajax", (req, res) => {
if (!res.locals.currentUser) {
message.create({
name: req.body.anonim,
uid: null,
uav: req.body.avatar,
text: req.body.text,
});
} else {
message.create({
name: res.locals.currentUser.username,
uid: res.locals.currentUser._id,
uav: res.locals.currentUser.avatar,
text: req.body.text,
});
}
res.sendStatus(200);
});
//AUXILIAR
//USERS TABLE
app.get("/users", isLoggedIn, (req, res) => {
user.find({}, (err, usr) => {
if (err) throw err;
res.render("users", { usr: usr });
});
});
//reset messages
app.get("/dropM", (req, res) => {
mongoose.connection.collection("messages").drop();
res.redirect("/");
});
app.get("/dropU", (req, res) => {
mongoose.connection.collection("users").drop();
res.redirect("/");
});
//middleware to check if a user is logged in
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect("/login");
}
const port = 3333;
app.listen(port, () => {
console.log(`DRICT listening to port - ${port}`);
});