-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
211 lines (207 loc) · 7.17 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//impoting libraries and configuring them
const express = require("express");
const BSON = require('bson');
const session = require('express-session')
const app = express();
let alert = require('alert');
const bcrypt = require('bcrypt');
const path = require('path');
const ejsMate = require('ejs-mate');
const mongoose = require("mongoose");
const User = require('./views/models/users')
const BlogPost = require('./views/models/post')
app.use(express.urlencoded({encoded: true}))
app.engine('ejs',ejsMate)
// Declaring Folders for css and image files
app.use( express.static( "public" ) );
app.use( express.static("views/CSS") );
const secret = process.env.SECRET || "Portfolio";
app.use(session({secret : secret}))
//Setting Up DataBase Connections
const MongoClient = require('mongodb').MongoClient;
const uri = process.env.DB_URL || "mongodb+srv://CodingBlood:K@[email protected]/myFirstDatabase?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
const collection = client.db("Portfolio").collection("UserData");
// perform actions on the collection object
});
// User Authentication via Hashing and Salting
// const hashpassword = async(pw)=>{
// const salt = await bcrypt.genSalt(12);
// const hash = await bcrypt.hash(pw, salt);
// }
// const login = async(pw, hashpassword)=>{
// const result = await bcrypt.compare(pw, hashpassword)
// if(result){
// console.log("Match");
// }else{
// console.log("TRY AGAIN");
// }
// }
// login("monkey","$2b$10$1V5zFenClUv1S80NL80Nx.3fi.G7/shWdVRzLPFE0poXdDM8RFFce")
// Routes
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname,'views'))
app.get('/', (req,res)=>{
res.render('index')
})
app.post('/Login', async(req,res)=>{
const {uname, pass} = req.body;
const query = {
username: uname,
};
const collection = client.db("Portfolio").collection("UserData");
const udetail = await collection.findOne(query);
const login = async(pw, hashpassword)=>{
const result = await bcrypt.compare(pw, hashpassword)
if(result){
req.session.username=uname;
res.redirect('/Secret/' + uname)
}else{
console.log("TRY AGAIN");
alert("Wrong UserID or Password");
}
}
login(req.body.pass,udetail.password);
})
app.get('/Login', (req,res)=>{
res.render('login')
})
app.post('/SignUp', async(req,res)=>{
const {fname , lname, email, uname, pass} = req.body;
const salt = await bcrypt.genSalt(12);
const hash = await bcrypt.hash(pass, salt);
const user = new User({
username: uname,
password : hash,
fname: fname,
lname: lname,
email: email
})
const collection = client.db("Portfolio").collection("UserData");
const result = await collection.insertOne(user);
req.session.username=uname;
res.redirect('/Secret/' + uname)
})
app.get('/SignUp',(req,res)=>{
res.render('signup')
})
app.get('/SignOut',(req,res)=>{
req.session.username=null;
res.redirect('/');
})
app.get('/Secret/:username',async(req,res)=>{
if(!req.session.username){
res.redirect('/Login');
}else{
const query = {
username: req.params.username,
};
const collection = client.db("Portfolio").collection("UserData");
const udetail = await collection.findOne(query);
const post_collection = client.db("Portfolio").collection("BlogPost");
const post_query = {
username: req.params.username
};
const post_details = await post_collection.find(post_query).sort({"Stars": 1});
const allValues = await post_details.toArray();
res.render('ControlCenter',{fname:udetail.fname,lname:udetail.lname,uname:udetail.username,postdetail:allValues.reverse()});
}
})
app.get('/Secret',async(req,res)=>{
if(!req.session.username){
res.redirect('/Login');
}else{
res.redirect('/Secret/' + req.session.username)
}
})
app.get('/TBlogs',async(req,res)=>{
const post_collection = client.db("Portfolio").collection("BlogPost");
const post_details = await post_collection.find({}).sort({"Stars": 1});
const allValues = await post_details.toArray();
res.render('TBlogs',{uname:req.session.username,postdetail:allValues.reverse()});
})
app.get('/GBlogs',async(req,res)=>{
const post_collection = client.db("Portfolio").collection("BlogPost");
const post_details = await post_collection.find({})
const allValues = await post_details.toArray();
res.render('TBlogs',{uname:req.session.username,postdetail:allValues.reverse()});
})
app.post('/Star/:id/:username',async(req,res)=>{
if(!req.session.username){
res.redirect('/Login');
}else{
const post_collection = client.db("Portfolio").collection("BlogPost");
post_collection.updateOne({_id:BSON.ObjectId(req.params.id)},{
$inc: {
Stars: +1
}
}, (err,result) => {
if(err) console.log(err);
console.log(result)
});
//
// post_collection.findOne({_id:BSON.ObjectId(req.params.id)}, (err, post) => {
// if(post){
// console.log("*VVHBK&*T^%", post)
// }
// if(err){
// console.log(err)
// }
// });
// itemsCollection.updateOne(query, update, options)
// const allValues = await post_details.toArray();
// console.log(allValues)
// res.redirect('/Secret/' + req.params.username)
res.redirect('back')
}
})
app.post('/DownStar/:id/:username',async(req,res)=>{
if(!req.session.username){
res.redirect('/Login');
}else{
const post_collection = client.db("Portfolio").collection("BlogPost");
post_collection.updateOne({_id:BSON.ObjectId(req.params.id)},{
$inc: {
DownStars: +1
}
}, (err,result) => {
if(err) console.log(err);
console.log(result)
});
//
// post_collection.findOne({_id:BSON.ObjectId(req.params.id)}, (err, post) => {
// if(post){
// console.log("*VVHBK&*T^%", post)
// }
// if(err){
// console.log(err)
// }
// });
// itemsCollection.updateOne(query, update, options)
// const allValues = await post_details.toArray();
// console.log(allValues)
// res.redirect('/Secret/' + req.params.username)
res.redirect('back')
}
})
app.post('/Post/:username', async(req,res)=>{
const {desc , tittle} = req.body;
console.log("yooo"+req.body)
const blogpost = new BlogPost({
username: req.params.username,
Tittle : tittle,
Description: desc,
Stars : 0,
DownStars : 0,
visibility : 'v'
})
const collection = client.db("Portfolio").collection("BlogPost");
const result = await collection.insertOne(blogpost);
const add= '/Secret/'+req.params.username
res.redirect(add)
})
const port = process.env.PORT || 3000
app.listen(port, ()=>{
console.log("Server Started!")
})