-
Notifications
You must be signed in to change notification settings - Fork 0
/
Userroutes.js
155 lines (120 loc) · 4.63 KB
/
Userroutes.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
module.exports = function(app, passport) {
// =====================================
// HOME PAGE (with login links) ========
// =====================================
var Booking = require('./models/Booking');
app.get('/', function(req, res) {
if(req.isAuthenticated()) {
res.render('loginPage.ejs', { messages: "loggedIn" });
}else {
var flashMessage = req.flash('loginMessage');
console.log("message " + flashMessage);
res.render('loginPage.ejs', { messages: flashMessage });
}
// load the index.ejs file
});
app.get(`/userProfile`, isLoggedIn, function(req, res){
var user = req.user;
res.render(`userProfile.ejs`, {messages: "loggedIn", user});
});
// =====================================
// LOGIN ===============================
// =====================================
// show the login form
app.get('/loginUser', function(req, res) {
// render the page and pass in any flash data if it exists
res.render('loginPage.ejs', { message: req.flash('loginMessage') });
});
// process the login form
// app.post('/login', do all our passport stuff here);
// =====================================
// SIGNUP ==============================
// =====================================
// show the signup form
/*app.get('/signup', function(req, res) {
// render the page and pass in any flash data if it exists
res.render('loginPage.ejs', { message: req.flash('signupMessage') });
});*/
// =====================================
// LOGOUT ==============================
// =====================================
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
app.get('/sport', function(req, res) {
res.render('sport.ejs', { messages: null });
});
app.get('/user', isLoggedIn,function(req, res) {
var user = req.user;
//var bookids = user.bookings;
// console.log(user);
User.findOne({_id: user._id}).populate('bookings').populate( { path: "bookings", populate: { path: 'venue'} }).exec( function(err, users) {
if(err) {
console.log("not populating cuz" + err);
} else {
console.log(users);
res.render('userPage.ejs', { messages: "loggedIn" , users });
}
});
});
app.post('/user/update', function(req, res) {
var name = req.body.name;
var area = req.body.area;
User.update({ "_id": req.user._id }, { "name": name, "area": area }, function(err, data) {
if(!err) {
console.log("User updated");
res.redirect('/user');
}
else {
console.log("Not updated");
res.redirect('/user');
}
}) ;
})
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/user', // redirect to the secure profile section
failureRedirect : '/', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/user', // redirect to the secure profile section
failureRedirect : '/', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
app.post('/register', function(req, res) {
var currentUser = new UserKitty( {
name: req.body.username,
password: req.body.password,
username: req.body.email
})
});
app.get('/delete-:id',function(req, res) {
//console.log(req.params.id);
var id = req.params.id;
User.remove({_id: id}, function(err, doc) {
if(err) {
res.send("User not removed, go back");
} else {
req.logout();
res.redirect('/');
}
})
});
/*/*createUser(currentUser, function(err) {
if(err) {
res.send("the following error Occured: " + err);
}
else {
res.redirect("/user");
}
}); */
}
// route middleware to make sure a user is logged in
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/');
}