-
Notifications
You must be signed in to change notification settings - Fork 0
/
app (2).js
233 lines (195 loc) · 6.08 KB
/
app (2).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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const express=require('express');
const app=express();
const PORT=8080;
//session requirements
const session=require('express-session');
//middleware for login
const {isLoggedIn}=require("./middleware.js");
//authentication requirements
const passport=require('passport');
const LocalStrategy=require('passport-local');
//model requirements
const User=require("./models/user.js");
const Community=require("./models/community.js");
const AlertBox=require('./models/alertbox.js');
const path=require('path');
const methodOverride=require('method-override');
//flash for messages
const flash=require('connect-flash');
app.use(flash());
//sessions
//session
const sessionOptions={
secret:'supersecretcode',
resave:false,
saveUninitialized :true,
cookie:{
expires:Date.now()+7*24*60*60*1000,
maxAge:7*24*60*60*1000,
httpOnly:true,
}
}
app.use(session(sessionOptions));
//all the requirements for ejs
const ejsMate=require('ejs-mate');
app.set('view engine','ejs');
app.set("views",path.join(__dirname,"views"));
app.use(express.urlencoded({extended:true}));
app.use(methodOverride("_method"));
app.engine('ejs',ejsMate);
app.use(express.static(path.join(__dirname,"/public")))
//for user autentication
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//for mongodb
const mongoose = require('mongoose');
const MONGO_URL="mongodb+srv://yadlamaniymn2005:[email protected]/CM";
main().then(()=>{
console.log("DB is runing");
})
.catch(err => console.log(err));
async function main() {
await mongoose.connect(MONGO_URL);
}
//for local variables
app.use((req,res,next)=>{
res.locals.success=req.flash('success');
res.locals.error=req.flash('error');
res.locals.currUser=req.user;
next();
});
//for port of the express
app.listen(PORT,()=>{
console.log(`listening on ${PORT}`);
})
app.get("/",(req,res)=>{
res.render("./home.ejs");
})
app.get("/login",(req,res)=>{
res.render("./login.ejs");
})
app.post("/login",passport.authenticate('local',{failureRedirect:'/login',
failureFlash: true}),(req,res)=>{
res.redirect("/home");
})
app.get('/signup',(req,res)=>{
res.render("./signup.ejs");
})
app.post('/signup',async(req,res)=>{
let {username,email,password,phno}=req.body;
const newUser=new User({email,username,phno});
const registeredUser=await User.register(newUser,password);
console.log(registeredUser);
req.login(registeredUser,(err)=>{
if(err){
req.flash('error',"Invalid details");
}
res.redirect("/login");
});
});
app.get('/logout',(req,res)=>{
req.logout((err)=>{
if(err){
return next(err);
}
req.flash("success","you are looged out!");
res.redirect("/login");
})
})
// app.get('/login-fail',(req,res)=>{
// req.flash('error',"Invalid username or password");
// res.render("./login-fail.ejs");
// })
//to get about section
app.get("/home",(req,res)=>{
res.render("./home.ejs");
})
//Add community
app.get('/addCommunity',(req,res)=>{
res.render("./addCommunity.ejs");
})
app.post('/community',async(req,res)=>{
console.log(req.body);
const newCommunity=new Community(req.body);
newCommunity.owner=req.user._id;
newCommunity.resident.push(req.user);
console.log(newCommunity);
await newCommunity.save();
req.flash("success","New Community created!");
const newAlertBox=new AlertBox();
newAlertBox.community=newCommunity;
await newAlertBox.save();
res.redirect("/home");
});
//showing all the communities
app.get('/community',async (req,res)=>{
const allCommunity= await Community.find({});
res.render("index.ejs",{allCommunity});
})
//show route
app.get('/community/:id',async(req,res)=>{
let {id}=req.params;
const community=await Community.findById(id).populate('resident').populate("owner");
if(!req.user){
res.redirect('/login');
}
if(!community){
req.flash("error","NO such community exits");
}
if(!req.user){
res.render("/login.ejs");
}
const user=req.user;
const flag= community.resident.some(resi => resi.username === user.username);
console.log(flag);
const alertbox=await AlertBox.find({community}).populate('messages.user');;
const residents=community.resident;
const myalertbox=alertbox[0];
console.log(myalertbox);
const messagesAndUsers = myalertbox.messages.map(message => ({
message: message.msg,
user: message.user
}));
console.log(messagesAndUsers);
res.render("./show.ejs",{community,flag,residents,myalertbox,messagesAndUsers});
})
//join the community
app.post("/add/:id",async(req,res)=>{
let {id}=req.params;
const community=await Community.findById(id).populate('resident');
community.resident.push(req.user);
await community.save();
res.redirect(`/community/${id}`);
})
//my community
app.use("/mycommunity",isLoggedIn,async (req,res)=>{
const allCommunity= await Community.find({}).populate('resident').populate("owner");
const user=req.user;
const joinedCommunities = [];
for (const community of allCommunity) {
const flag= community.resident.some(resi => resi.username === user.username);
if(flag){
joinedCommunities.push(community);
}
}
res.render("./myCommunity.ejs",{joinedCommunities});
})
//to added a message to the alert box
app.post("/alertbox",async(req,res)=>{
const {id,msg}=req.body;
const userId=req.user._id;
const alertbox=await AlertBox.findById(id);
alertbox.messages.push({user:userId,msg:msg});
console.log(alertbox);
await alertbox.save();
res.redirect('/mycommunity');
})
app.use((err,req,res,next)=>{
let {status,message}=err;
res.status(status).render("./erros.ejs",{message});
next();
// res.status(status).send(message);
})