-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
141 lines (118 loc) · 3.62 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
const express = require("express");
const path = require("path");
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
const cookieParser = require("cookie-parser");
const upload = require("./config/multerconfig");
const userModel = require("./models/user");
const postModel = require("./models/post");
const app = express();
app.set("view engine", "ejs");
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));
app.use(cookieParser());
app.get("/", (req, res) => {
res.render("index");
});
app.get("/profile/upload", (req, res) => {
res.render("profileupload");
});
app.post("/upload", isLoggedIn, upload.single("image"), async (req, res) => {
let user = await userModel.findOne({ email: req.user.email });
user.profilepic = req.file.filename;
await user.save();
res.redirect("/profile");
});
app.get("/login", (req, res) => {
res.render("login");
});
app.get("/profile", isLoggedIn, async (req, res) => {
let user = await userModel
.findOne({ email: req.user.email })
.populate("posts");
res.render("profile", {
user: user,
});
});
app.get("/like/:id", isLoggedIn, async (req, res) => {
let post = await postModel
.findOne({ _id: req.params.id })
.populate("username");
if (post.likes.indexOf(req.user.userid) === -1) {
post.likes.push(req.user.userid);
} else {
post.likes.splice(post.likes.indexOf(req.user.userid), 1);
}
await post.save();
res.redirect("/profile");
});
app.get("/edit/:id", isLoggedIn, async (req, res) => {
let post = await postModel
.findOne({ _id: req.params.id })
.populate("username");
res.render("edit", {
post: post,
});
});
app.post("/update/:id", isLoggedIn, async (req, res) => {
let post = await postModel.findOneAndUpdate(
{ _id: req.params.id },
{ content: req.body.content },
{ new: true }
);
res.redirect("/profile");
});
app.post("/post", isLoggedIn, async (req, res) => {
let { content } = req.body;
let user = await userModel.findOne({ email: req.user.email });
let post = await postModel.create({ user: user._id, content: content });
user.posts.push(post._id);
await user.save();
res.redirect("/profile");
});
app.post("/register", async (req, res) => {
let { name, username, email, password, age } = req.body;
let user = await userModel.findOne({ email: email });
if (user) return res.status(500).send(`User already registered...`);
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(password, salt, async (err, hash) => {
let user = await userModel.create({
name: name,
username: username,
email: email,
password: hash,
age: age,
});
let token = jwt.sign({ email: email, userid: user._id }, "secret");
res.cookie("token", token);
res.send(`User registered...`);
});
});
});
app.post("/login", async (req, res) => {
let { email, password } = req.body;
let user = await userModel.findOne({ email: email });
if (!user) return res.status(500).send(`Email not found...`);
bcrypt.compare(password, user.password, (err, result) => {
if (result) {
let token = jwt.sign({ email: email, userid: user._id }, "secret");
res.cookie("token", token);
return res.redirect("/profile");
}
res.redirect("/login");
});
});
app.get("/logout", (req, res) => {
res.cookie("token", "");
res.redirect("/login");
});
function isLoggedIn(req, res, next) {
if (req.cookies.token === "") res.redirect("/login");
else {
let data = jwt.verify(req.cookies.token, "secret");
req.user = data;
next();
}
}
app.listen(3000);