-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
196 lines (184 loc) · 4.92 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
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
var express =require("express"),
app =express(),
bodyParser =require("body-parser"),
firebase = require("firebase"),
methodOverride =require("method-override"),
flash =require("connect-flash");
var config = {
// FIREBASE CONFIGURATION
};
firebase.initializeApp(config);
var db = firebase.database();
app.use(flash());
app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname+"/public"));
app.use(methodOverride("_method"));
app.use(require("express-session")({
secret:"Jai hind",
resave:false,
saveUniniatialized:false
}));
app.use(function(req,res,next){
res.locals.user=firebase.auth().currentUser;
res.locals.error=req.flash("error");
res.locals.success=req.flash("success");
next();
});
var ref;
// HOME
app.get("/",function(req,res){
res.render("home");
});
// TEAM
app.get("/author",function(req,res){
res.render("author");
});
// EVENTS
app.get("/events",function(req,res){
ref=db.ref('/events/');
var events=[];
ref.once('value',function(snapshot){
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
events.push(childData);
});
res.render("events",{events:events});
})
});
// NEW EVENT FORM
app.get("/new",isLoggedIn,function(req,res){
res.render("new");
});
// CREATE EVENT
app.post("/new",isLoggedIn,function(req,res){
var id = db.ref().child('events').push().key;
var event=req.body.event;
event.id=id;
event.author=firebase.auth().currentUser.email;
var updates = {};
updates['/events/' + id] = event;
db.ref().update(updates);
console.log(event);
res.redirect("/events");
});
// EDIT
app.get("/events/:id/edit",isOwner,function(req,res){
ref=db.ref('/events/'+req.params.id);
ref.once('value',function(snapshot){
var event=snapshot.val();
console.log(event);
res.render("edit",{event:event});
})
})
// UPDATE EVENT
app.put("/events/:id",isOwner,function(req,res){
ref=db.ref('/events/'+req.params.id);
var event=req.body.event;
event.id=req.params.id;
db.ref('/events/'+req.params.id).set(event);
res.redirect('/events');
})
// DELETE EVENT
app.delete("/events/:id/delete",isOwner,function(req,res){
ref = db.ref("/events/"+req.params.id);
ref.remove();
res.redirect("/events");
})
// APPLY
app.get("/events/:id/apply",function(req,res){
ref=db.ref('/events/'+req.params.id);
ref.once('value',function(snapshot){
var event=snapshot.val();
console.log(event);
res.render("apply",{event:event});
})
})
app.post("/events/:id",function(req,res){
var pid = db.ref('/events/:id').push().key;
var participant=req.body.participant;
participant.pid=pid;
var updates = {};
updates['/events/' + req.params.id+'/participants/'+pid] = participant;
db.ref().update(updates);
if(participant){
req.flash("success","Successfully applied!!");
}
else{
req.flash("error","Something went wrong!!");
}
res.redirect("/events");
})
// LOGIN PAGE
app.get("/login",function(req,res){
res.render("login");
});
// LOGGING IN
app.post("/login",function(req,res){
firebase.auth().signInWithEmailAndPassword(req.body.email, req.body.password)
.then(function(userRecord){
res.redirect("/");
})
.catch(function(err) {
console.log(err);
res.redirect("/login");
});
})
// SIGN UP
app.get("/register",function(req,res){
res.render("register");
});
// CREATE NEW USER
app.post("/register",function(req,res){
firebase.auth().createUserWithEmailAndPassword(req.body.email,req.body.password)
.then(function(userRecord) {
res.redirect("/");
})
.catch(function(err){
console.log(error.code);
console.log(error.message);
res.redirect("/register")
})
});
// LOGOUT
app.get("/logout",function(req,res){
firebase.auth().signOut().then(function() {
res.redirect("/");
}, function(error) {
console.log(error);
res.redirect("/");
});
})
// MIDDLEWARE
function isLoggedIn(req, res, next) {
var user = firebase.auth().currentUser;
if (user !== null) {
req.user = user;
next();
} else {
res.redirect('/login');
}
}
function isOwner(req,res,next){
var user = firebase.auth().currentUser;
if (user !== null) {
var ref=db.ref('/events/'+req.params.id);
ref.once('value',function(snapshot){
console.log( snapshot.val().email)
if(firebase.auth().currentUser.email === snapshot.val().author){
req.user = user;
next();
}
else{
res.redirect("/events");
}
})
}
else {
res.redirect('/login');
}
}
app.listen(process.env.PORT,function(){
console.log("SERVER STARTED!!!");
});