-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
164 lines (124 loc) · 4.07 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const express = require('express');
const app = express();
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const crypto = require('crypto');
const path = require('path');
const userModel = require('./models/user');
const postModel = require('./models/post')
const cookieParser = require('cookie-parser');
const multer = require('multer');
const { posts } = require('moongose/models');
app.set("view engine", "ejs");
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(cookieParser());
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/images/uploads')
},
filename: function (req, file, cb) {
crypto.randomBytes(12 , function(err, bytes){
const fn = bytes.toString("hex") + path.extname(file.originalname);
cb(null, fn);
})
}
})
const upload = multer({ storage: storage })
app.get('/',function(req,res){
res.render('index');
})
app.get('/file',function(req,res){
res.render('file');
})
app.post('/upload', upload.single('file'),function(req,res){
console.log(req.file);
res.render('upload');
})
app.get('/login',function(req,res){
res.render('login');
})
app.post('/register',async (req,res)=>{
let {username, name, age, email, password} = req.body;
let user = await userModel.findOne({email})
if(user) return res.status(500).send("User already created");
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(password, salt, async (err, hash)=>{
// console.log(hash)
let user = await userModel.create({
username,
name,
age,
email,
password:hash
});
let token = jwt.sign({email: email, userid: user._id}, "ahhahahah");
res.cookie("token",token);
res.send("Created...")
});
})
})
app.post('/login',async (req,res)=>{
let {username, name, age, email, password} = req.body;
let user = await userModel.findOne({email})
if(!user) return res.status(500).send("Something Went Wrong");
bcrypt.compare(password, user.password, function(err, result){
if(result) {
let token = jwt.sign({email: email, userid: user._id}, "ahhahahah");
res.cookie("token",token);
res.status(200).redirect("profile");
}
else return res.redirect("/login")
})
})
app.get('/logout',function(req,res){
res.cookie("token","");
res.redirect("/login")
})
app.get('/profile', isLoggedIn , async function(req,res){
let user = await userModel.findOne({email: req.user.email}).populate("posts");
// console.log(user)
res.render("profile", {user})
});
app.get('/like/:id', isLoggedIn , async function(req,res){
let post = await postModel.findOne({_id: req.params.id}).populate("user");
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 function(req,res){
let post = await postModel.findOne({_id: req.params.id}).populate("user");
res.render("edit", {post});
})
app.post('/update/:id', isLoggedIn , async function(req,res){
let post = await postModel.findOneAndUpdate({_id: req.params.id}, {content: req.body.content});
res.redirect("/profile");
})
app.post('/post', isLoggedIn , async function(req,res){
let user = await userModel.findOne({email: req.user.email});
// console.log(user)
let {content} = req.body;
let post = await postModel.create({
user: user._id,
content
});
user.posts.push(post._id);
await user.save();
res.redirect("/profile")
})
function isLoggedIn(req,res, next){
if (req.cookies.token === "") {
res.redirect("/login")
}
else{
let data = jwt.verify(req.cookies.token, "ahhahahah");
req.user = data;
next();
}
}
app.listen(3000);