2016-05-31 21:54:21 -06:00
|
|
|
var router = require('express').Router(),
|
|
|
|
mw = require('../middleware.js'),
|
|
|
|
secret = require('../secrets.js'),
|
2016-06-12 18:18:34 -06:00
|
|
|
slug = require('slug'),
|
2016-05-31 21:54:21 -06:00
|
|
|
User = require('../models/user.js');
|
|
|
|
|
|
|
|
// Show map
|
|
|
|
router.get('/:slug?', function(req,res,next){
|
|
|
|
var mapuser='', user='', cbc=0;
|
|
|
|
|
|
|
|
// Confirm sucessful queries
|
|
|
|
function checkQuery(err,found) {
|
|
|
|
if (err){ mw.throwErr(req,err); }
|
2016-09-08 05:26:12 -06:00
|
|
|
if (found){ return found; }
|
2016-05-31 21:54:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call renderMap() on completion
|
|
|
|
function checkCBC() {
|
|
|
|
cbc+=1;
|
|
|
|
if (cbc>1){ renderMap(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
// QUERIES
|
|
|
|
// Get logged in user -> user
|
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
User.findById(req.session.passport.user, function(err, found) {
|
|
|
|
user = checkQuery(err,found);
|
|
|
|
checkCBC();
|
|
|
|
});
|
|
|
|
} else { checkCBC(); }
|
|
|
|
// Get tracked user -> mapuser
|
|
|
|
if (req.params.slug) {
|
|
|
|
User.findOne({slug:req.params.slug}, function(err, found) {
|
|
|
|
mapuser = checkQuery(err,found);
|
|
|
|
checkCBC();
|
|
|
|
});
|
|
|
|
} else { checkCBC(); }
|
|
|
|
|
|
|
|
// Show map
|
|
|
|
function renderMap() {
|
|
|
|
// GET /map shows logged-in user's map
|
2016-09-08 05:26:12 -06:00
|
|
|
if (!mapuser && !user) {
|
2016-05-31 21:54:21 -06:00
|
|
|
res.redirect('/');
|
|
|
|
} else {
|
2016-09-09 21:55:53 -06:00
|
|
|
if (user && !mapuser) { mapuser = user; }
|
2016-05-31 21:54:21 -06:00
|
|
|
res.render('map.html', {
|
|
|
|
api: secret.mapAPI,
|
|
|
|
mapuser: mapuser,
|
|
|
|
user: user,
|
|
|
|
noFooter: '1',
|
|
|
|
noHeader: (req.query.noheader)?req.query.noheader.match(/\d/)[0]:'',
|
|
|
|
disp: (req.query.disp)?req.query.disp.match(/\d/)[0]:'' // 0=map, 1=streetview, 2=both
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-06-12 18:18:34 -06:00
|
|
|
router.post('/:slug?', mw.ensureAuth, function(req,res,next){
|
|
|
|
// Set new user settings
|
|
|
|
User.findByIdAndUpdate(req.session.passport.user, {$set:{name: req.body.name,
|
|
|
|
slug: slug(req.body.slug),
|
2016-08-10 03:15:16 -06:00
|
|
|
email: req.body.email,
|
2016-06-12 18:18:34 -06:00
|
|
|
settings: {
|
|
|
|
units: req.body.units,
|
|
|
|
defaultMap: req.body.map,
|
|
|
|
defaultZoom: req.body.zoom,
|
|
|
|
showSpeed: (req.body.showSpeed)?true:false,
|
|
|
|
showAlt: (req.body.showAlt)?true:false,
|
|
|
|
showStreetview: (req.body.showStreet)?true:false
|
|
|
|
}
|
|
|
|
}}, function(err, user){
|
|
|
|
if (err) { console.log('Error updating user settings:',err); mw.throwErr(req,err); }
|
|
|
|
else { req.flash('success', 'Settings updated. '); }
|
|
|
|
res.redirect('/map#');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-10 00:17:37 -06:00
|
|
|
router.delete('/:slug?', mw.ensureAuth, function(req,res,next){
|
|
|
|
// Delete user account
|
|
|
|
User.findByIdAndRemove(
|
|
|
|
req.session.passport.user,
|
|
|
|
function(err) {
|
2016-08-10 03:02:14 -06:00
|
|
|
if (err) {
|
|
|
|
console.log('Error deleting user:',err);
|
|
|
|
mw.throwErr(req,err);
|
|
|
|
} else {
|
|
|
|
req.flash('success', 'Your account has been deleted. ');
|
|
|
|
res.sendStatus(200);
|
|
|
|
}
|
2016-08-10 00:17:37 -06:00
|
|
|
}
|
|
|
|
)
|
2016-08-10 00:18:38 -06:00
|
|
|
});
|
2016-06-12 18:18:34 -06:00
|
|
|
|
2016-05-31 21:54:21 -06:00
|
|
|
// Redirect /id/ to /slug/
|
|
|
|
router.get('/id/:id', function(req,res,next){
|
|
|
|
User.findById(req.params.id, function(err, user){
|
|
|
|
if (err){ mw.throwErr(req,err); }
|
|
|
|
if (!user){ next(); }
|
|
|
|
else { res.redirect('/map/'+user.slug+((req.url.indexOf('?')<0)?'':('?'+req.url.split('?')[1]))); }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|