-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
66 lines (62 loc) · 2.04 KB
/
routes.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
var passport = require('passport');
var User = require('./models/user');
module.exports = function(app){
console.log('in routes');
app.get("/", function (req,res){
if(!req.user){
console.log('needs login');
res.redirect('/login');
/*User.findOneAndUpdate({username : 'ichauster'},
{$set:{username:'iChauster'}},
function (err, numberAffected, raw){
res.render('index',{user : req.user});
}); */
}else{
console.log('user in');
User.find({username : req.user.username}).lean().exec(function (err, docs){
if (err){
console.log(err);
}
var js = JSON.stringify(docs);
console.log(js);
console.log('User-Agent: ' + req.headers['user-agent']);
var userAgent = req.headers['user-agent'];
if(userAgent.indexOf('Goalr') > -1 || userAgent.indexOf('iPhone') > -1){
res.send(js);
}else{
res.render('index',{user : req.user, docs : js});
}
});
}
});
app.get('/register', function(req, res){
res.render('register',{});
})
app.post('/register', function(req, res) {
User.register(new User({ username : req.body.username, firstName : req.body.firstName, lastName : req.body.lastName, email : req.body.email, goalName : [{goal : "coder", subgoals : [{goal : ['code once a day', false], date : Date()}], dateDue:Date()}]}), req.body.password, function(err, account) {
if (err) {
console.log(err);
return res.render("register", {info: "Sorry. That username already exists. Try again."});
}
passport.authenticate('local')(req, res, function () {
res.redirect('/');
});
});
});
app.get('/login', function(req, res) {
res.render('login', { user : req.user });
});
app.post('/login', passport.authenticate('local'), function(req, res) {
res.redirect('/')
});
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
app.get('/info', function(req,res){
res.render('info');
});
app.get('/managr', function(req,res){
res.render('managr');
});
}