2015-10-02 03:55:55 +00:00
|
|
|
import dedent from 'dedent';
|
2015-08-20 16:40:03 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
import debugFactory from 'debug';
|
2015-08-07 20:31:48 +00:00
|
|
|
|
2015-10-02 03:55:55 +00:00
|
|
|
import { ifNoUser401, ifNoUserRedirectTo } from '../utils/middleware';
|
2015-08-07 20:31:48 +00:00
|
|
|
|
2015-08-20 16:40:03 +00:00
|
|
|
const debug = debugFactory('freecc:boot:user');
|
2015-08-19 20:05:53 +00:00
|
|
|
const daysBetween = 1.5;
|
2015-10-02 03:55:55 +00:00
|
|
|
const sendNonUserToMap = ifNoUserRedirectTo('/map');
|
2015-08-19 20:05:53 +00:00
|
|
|
|
2015-08-07 20:31:48 +00:00
|
|
|
function calcCurrentStreak(cals) {
|
2015-08-19 23:10:59 +00:00
|
|
|
const revCals = cals.concat([Date.now()]).slice().reverse();
|
2015-08-07 20:31:48 +00:00
|
|
|
let streakBroken = false;
|
2015-08-19 20:05:53 +00:00
|
|
|
const lastDayInStreak = revCals
|
2015-08-07 20:31:48 +00:00
|
|
|
.reduce((current, cal, index) => {
|
2015-08-19 20:05:53 +00:00
|
|
|
const before = revCals[index === 0 ? 0 : index - 1];
|
2015-08-07 20:31:48 +00:00
|
|
|
if (
|
|
|
|
!streakBroken &&
|
2015-08-19 20:05:53 +00:00
|
|
|
moment(before).diff(cal, 'days', true) < daysBetween
|
2015-08-07 20:31:48 +00:00
|
|
|
) {
|
2015-08-19 20:05:53 +00:00
|
|
|
return index;
|
2015-08-07 20:31:48 +00:00
|
|
|
}
|
2015-08-19 20:45:32 +00:00
|
|
|
streakBroken = true;
|
2015-08-19 20:05:53 +00:00
|
|
|
return current;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
const lastTimestamp = revCals[lastDayInStreak];
|
|
|
|
return Math.ceil(moment().diff(lastTimestamp, 'days', true));
|
|
|
|
}
|
|
|
|
|
2015-08-19 22:57:29 +00:00
|
|
|
function calcLongestStreak(cals) {
|
|
|
|
let tail = cals[0];
|
|
|
|
const longest = cals.reduce((longest, head, index) => {
|
|
|
|
const last = cals[index === 0 ? 0 : index - 1];
|
|
|
|
// is streak broken
|
|
|
|
if (moment(head).diff(last, 'days', true) > daysBetween) {
|
|
|
|
tail = head;
|
|
|
|
}
|
|
|
|
if (dayDiff(longest) < dayDiff([head, tail])) {
|
|
|
|
return [head, tail];
|
|
|
|
}
|
|
|
|
return longest;
|
|
|
|
}, [cals[0], cals[0]]);
|
|
|
|
|
|
|
|
return Math.ceil(dayDiff(longest));
|
|
|
|
}
|
|
|
|
|
|
|
|
function dayDiff([head, tail]) {
|
|
|
|
return moment(head).diff(tail, 'days', true);
|
2015-08-07 20:31:48 +00:00
|
|
|
}
|
2015-05-25 23:02:09 +00:00
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
module.exports = function(app) {
|
2015-06-03 23:31:42 +00:00
|
|
|
var router = app.loopback.Router();
|
2015-06-03 23:19:23 +00:00
|
|
|
var User = app.models.User;
|
2015-10-02 03:55:55 +00:00
|
|
|
// var Story = app.models.Story;
|
2013-11-14 07:29:55 +00:00
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
router.get('/login', function(req, res) {
|
|
|
|
res.redirect(301, '/signin');
|
|
|
|
});
|
|
|
|
router.get('/logout', function(req, res) {
|
|
|
|
res.redirect(301, '/signout');
|
|
|
|
});
|
|
|
|
router.get('/signin', getSignin);
|
|
|
|
router.get('/signout', signout);
|
|
|
|
router.get('/forgot', getForgot);
|
|
|
|
router.post('/forgot', postForgot);
|
2015-08-16 16:54:34 +00:00
|
|
|
router.get('/reset-password', getReset);
|
|
|
|
router.post('/reset-password', postReset);
|
2015-06-03 23:19:23 +00:00
|
|
|
router.get('/email-signup', getEmailSignup);
|
|
|
|
router.get('/email-signin', getEmailSignin);
|
2015-10-02 03:55:55 +00:00
|
|
|
router.get(
|
|
|
|
'/toggle-lockdown-mode',
|
|
|
|
sendNonUserToMap,
|
|
|
|
toggleLockdownMode
|
|
|
|
);
|
2015-08-20 16:40:03 +00:00
|
|
|
router.post(
|
|
|
|
'/account/delete',
|
|
|
|
ifNoUser401,
|
|
|
|
postDeleteAccount
|
|
|
|
);
|
2015-10-02 03:55:55 +00:00
|
|
|
router.get(
|
|
|
|
'/account',
|
|
|
|
sendNonUserToMap,
|
|
|
|
getAccount
|
|
|
|
);
|
2015-09-27 17:49:44 +00:00
|
|
|
router.get('/vote1', vote1);
|
2015-09-27 21:00:55 +00:00
|
|
|
router.get('/vote2', vote2);
|
2015-06-03 23:19:23 +00:00
|
|
|
// Ensure this is the last route!
|
|
|
|
router.get('/:username', returnUser);
|
|
|
|
|
2015-06-03 23:31:42 +00:00
|
|
|
app.use(router);
|
|
|
|
|
2015-07-23 06:27:18 +00:00
|
|
|
function getSignin(req, res) {
|
2015-06-03 23:19:23 +00:00
|
|
|
if (req.user) {
|
|
|
|
return res.redirect('/');
|
|
|
|
}
|
|
|
|
res.render('account/signin', {
|
|
|
|
title: 'Free Code Camp Login'
|
|
|
|
});
|
|
|
|
}
|
2014-01-13 09:34:54 +00:00
|
|
|
|
2015-07-23 06:27:18 +00:00
|
|
|
function signout(req, res) {
|
2015-06-03 23:19:23 +00:00
|
|
|
req.logout();
|
|
|
|
res.redirect('/');
|
2015-03-28 02:34:12 +00:00
|
|
|
}
|
2014-01-07 22:45:42 +00:00
|
|
|
|
2015-07-23 06:27:18 +00:00
|
|
|
function getEmailSignin(req, res) {
|
2015-06-03 23:19:23 +00:00
|
|
|
if (req.user) {
|
|
|
|
return res.redirect('/');
|
|
|
|
}
|
|
|
|
res.render('account/email-signin', {
|
|
|
|
title: 'Sign in to your Free Code Camp Account'
|
|
|
|
});
|
2015-05-21 18:07:40 +00:00
|
|
|
}
|
2014-01-07 22:45:42 +00:00
|
|
|
|
2015-07-23 06:27:18 +00:00
|
|
|
function getEmailSignup(req, res) {
|
2015-06-03 23:19:23 +00:00
|
|
|
if (req.user) {
|
|
|
|
return res.redirect('/');
|
|
|
|
}
|
|
|
|
res.render('account/email-signup', {
|
|
|
|
title: 'Create Your Free Code Camp Account'
|
2015-03-21 04:42:02 +00:00
|
|
|
});
|
|
|
|
}
|
2014-12-23 16:48:28 +00:00
|
|
|
|
2015-07-23 06:27:18 +00:00
|
|
|
function getAccount(req, res) {
|
2015-10-02 03:55:55 +00:00
|
|
|
const { username } = req.user;
|
|
|
|
return res.redirect('/' + username);
|
2015-06-03 23:19:23 +00:00
|
|
|
}
|
2015-01-24 09:14:41 +00:00
|
|
|
|
2015-07-23 06:27:18 +00:00
|
|
|
function returnUser(req, res, next) {
|
2015-08-05 20:01:19 +00:00
|
|
|
const username = req.params.username.toLowerCase();
|
|
|
|
const { path } = req;
|
2015-06-10 22:22:57 +00:00
|
|
|
User.findOne(
|
2015-08-05 20:01:19 +00:00
|
|
|
{ where: { username } },
|
2015-10-02 06:03:52 +00:00
|
|
|
function(err, profileUser) {
|
2015-06-03 23:19:23 +00:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2015-10-02 06:03:52 +00:00
|
|
|
if (!profileUser) {
|
2015-08-05 20:01:19 +00:00
|
|
|
req.flash('errors', {
|
|
|
|
msg: `404: We couldn't find path ${ path }`
|
|
|
|
});
|
|
|
|
return res.redirect('/');
|
|
|
|
}
|
|
|
|
|
2015-10-02 06:03:52 +00:00
|
|
|
var cals = profileUser
|
2015-08-07 20:31:48 +00:00
|
|
|
.progressTimestamps
|
|
|
|
.map(objOrNum => {
|
|
|
|
return typeof objOrNum === 'number' ?
|
|
|
|
objOrNum :
|
|
|
|
objOrNum.timestamp;
|
|
|
|
})
|
2015-08-19 20:05:53 +00:00
|
|
|
.sort();
|
2015-08-05 20:01:19 +00:00
|
|
|
|
2015-10-02 06:03:52 +00:00
|
|
|
profileUser.currentStreak = calcCurrentStreak(cals);
|
|
|
|
profileUser.longestStreak = calcLongestStreak(cals);
|
2015-08-05 20:01:19 +00:00
|
|
|
|
2015-10-02 06:03:52 +00:00
|
|
|
const data = profileUser
|
2015-08-07 20:31:48 +00:00
|
|
|
.progressTimestamps
|
|
|
|
.map((objOrNum) => {
|
|
|
|
return typeof objOrNum === 'number' ?
|
|
|
|
objOrNum :
|
|
|
|
objOrNum.timestamp;
|
|
|
|
})
|
2015-08-19 20:05:53 +00:00
|
|
|
.filter((timestamp) => {
|
|
|
|
return !!timestamp;
|
|
|
|
})
|
2015-08-07 20:31:48 +00:00
|
|
|
.reduce((data, timeStamp) => {
|
|
|
|
data[(timeStamp / 1000)] = 1;
|
|
|
|
return data;
|
|
|
|
}, {});
|
|
|
|
|
2015-10-02 06:03:52 +00:00
|
|
|
const baseAndZip = profileUser.completedChallenges.filter(
|
|
|
|
function(obj) {
|
2015-08-05 20:21:53 +00:00
|
|
|
return obj.challengeType === 3 || obj.challengeType === 4;
|
2015-10-02 06:03:52 +00:00
|
|
|
}
|
|
|
|
);
|
2015-08-05 20:01:19 +00:00
|
|
|
|
2015-10-02 06:03:52 +00:00
|
|
|
const bonfires = profileUser.completedChallenges.filter(function(obj) {
|
2015-08-19 02:23:43 +00:00
|
|
|
return obj.challengeType === 5 && (obj.name || '').match(/Bonfire/g);
|
2015-08-18 21:53:18 +00:00
|
|
|
});
|
|
|
|
|
2015-10-02 06:03:52 +00:00
|
|
|
const waypoints = profileUser.completedChallenges.filter(function(obj) {
|
|
|
|
return (obj.name || '').match(/^Waypoint/i);
|
|
|
|
});
|
|
|
|
|
2015-08-05 20:01:19 +00:00
|
|
|
res.render('account/show', {
|
2015-10-02 06:03:52 +00:00
|
|
|
title: 'Camper ' + profileUser.username + '\'s portfolio',
|
|
|
|
username: profileUser.username,
|
|
|
|
name: profileUser.name,
|
|
|
|
isMigrationGrandfathered: profileUser.isMigrationGrandfathered,
|
|
|
|
isGithubCool: profileUser.isGithubCool,
|
|
|
|
isLocked: !!profileUser.isLocked,
|
|
|
|
|
|
|
|
location: profileUser.location,
|
2015-08-05 20:01:19 +00:00
|
|
|
calender: data,
|
2015-10-02 06:03:52 +00:00
|
|
|
|
|
|
|
github: profileUser.githubURL,
|
|
|
|
linkedin: profileUser.linkedin,
|
|
|
|
google: profileUser.google,
|
|
|
|
facebook: profileUser.facebook,
|
|
|
|
twitter: profileUser.twitter,
|
|
|
|
picture: profileUser.picture,
|
|
|
|
|
|
|
|
progressTimestamps: profileUser.progressTimestamps,
|
|
|
|
|
|
|
|
baseAndZip,
|
|
|
|
bonfires,
|
|
|
|
waypoints,
|
|
|
|
moment,
|
|
|
|
|
|
|
|
longestStreak: profileUser.longestStreak,
|
|
|
|
currentStreak: profileUser.currentStreak
|
2015-08-05 20:01:19 +00:00
|
|
|
});
|
2015-05-21 18:07:40 +00:00
|
|
|
}
|
2015-06-03 23:19:23 +00:00
|
|
|
);
|
|
|
|
}
|
2014-01-28 22:41:13 +00:00
|
|
|
|
2015-10-02 03:55:55 +00:00
|
|
|
function toggleLockdownMode(req, res, next) {
|
2015-10-02 06:03:52 +00:00
|
|
|
if (req.user.isLocked === true) {
|
|
|
|
req.user.isLocked = false;
|
2015-10-02 03:55:55 +00:00
|
|
|
return req.user.save(function(err) {
|
|
|
|
if (err) { return next(err); }
|
2015-09-28 00:08:56 +00:00
|
|
|
|
2015-10-02 03:55:55 +00:00
|
|
|
req.flash('success', {
|
|
|
|
msg: dedent`
|
|
|
|
Other people can now view all your challenge solutions.
|
|
|
|
You can change this back at any time in the "Manage My Account"
|
|
|
|
section at the bottom of this page.
|
|
|
|
`
|
2015-09-28 00:08:56 +00:00
|
|
|
});
|
2015-10-02 03:55:55 +00:00
|
|
|
res.redirect('/' + req.user.username);
|
|
|
|
});
|
2015-09-28 00:08:56 +00:00
|
|
|
}
|
2015-10-02 06:03:52 +00:00
|
|
|
req.user.isLocked = true;
|
2015-10-02 03:55:55 +00:00
|
|
|
return req.user.save(function(err) {
|
|
|
|
if (err) { return next(err); }
|
|
|
|
|
|
|
|
req.flash('success', {
|
|
|
|
msg: dedent`
|
|
|
|
All your challenge solutions are now hidden from other people.
|
|
|
|
You can change this back at any time in the "Manage My Account"
|
|
|
|
section at the bottom of this page.
|
|
|
|
`
|
|
|
|
});
|
|
|
|
res.redirect('/' + req.user.username);
|
|
|
|
});
|
2015-09-28 00:08:56 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 06:27:18 +00:00
|
|
|
function postDeleteAccount(req, res, next) {
|
2015-06-03 23:19:23 +00:00
|
|
|
User.destroyById(req.user.id, function(err) {
|
2015-03-21 04:42:02 +00:00
|
|
|
if (err) { return next(err); }
|
2015-06-03 23:19:23 +00:00
|
|
|
req.logout();
|
|
|
|
req.flash('info', { msg: 'Your account has been deleted.' });
|
|
|
|
res.redirect('/');
|
2013-12-13 05:27:51 +00:00
|
|
|
});
|
2015-06-03 23:19:23 +00:00
|
|
|
}
|
2014-03-07 19:08:56 +00:00
|
|
|
|
2015-08-16 16:54:34 +00:00
|
|
|
function getReset(req, res) {
|
|
|
|
if (!req.accessToken) {
|
|
|
|
req.flash('errors', { msg: 'access token invalid' });
|
|
|
|
return res.render('account/forgot');
|
2015-06-03 23:19:23 +00:00
|
|
|
}
|
2015-08-16 16:54:34 +00:00
|
|
|
res.render('account/reset', {
|
|
|
|
title: 'Password Reset',
|
|
|
|
accessToken: req.accessToken.id
|
|
|
|
});
|
2015-03-21 04:42:02 +00:00
|
|
|
}
|
2014-03-07 19:08:56 +00:00
|
|
|
|
2015-07-23 06:27:18 +00:00
|
|
|
function postReset(req, res, next) {
|
2015-08-16 16:54:34 +00:00
|
|
|
const errors = req.validationErrors();
|
|
|
|
const { password } = req.body;
|
2015-03-21 04:42:02 +00:00
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
if (errors) {
|
|
|
|
req.flash('errors', errors);
|
|
|
|
return res.redirect('back');
|
|
|
|
}
|
|
|
|
|
2015-08-16 16:54:34 +00:00
|
|
|
User.findById(req.accessToken.userId, function(err, user) {
|
2015-06-03 23:19:23 +00:00
|
|
|
if (err) { return next(err); }
|
2015-08-16 16:54:34 +00:00
|
|
|
user.updateAttribute('password', password, function(err) {
|
|
|
|
if (err) { return next(err); }
|
|
|
|
|
|
|
|
debug('password reset processed successfully');
|
|
|
|
req.flash('info', { msg: 'password reset processed successfully' });
|
|
|
|
res.redirect('/');
|
|
|
|
});
|
2015-06-03 23:19:23 +00:00
|
|
|
});
|
|
|
|
}
|
2014-03-07 19:08:56 +00:00
|
|
|
|
2015-07-23 06:27:18 +00:00
|
|
|
function getForgot(req, res) {
|
2015-06-03 23:19:23 +00:00
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
return res.redirect('/');
|
|
|
|
}
|
|
|
|
res.render('account/forgot', {
|
|
|
|
title: 'Forgot Password'
|
|
|
|
});
|
2015-03-21 04:42:02 +00:00
|
|
|
}
|
2014-03-07 19:08:56 +00:00
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
/**
|
|
|
|
* POST /forgot
|
|
|
|
* Create a random token, then the send user an email with a reset link.
|
|
|
|
*/
|
2014-03-07 19:08:56 +00:00
|
|
|
|
2015-08-16 16:54:34 +00:00
|
|
|
function postForgot(req, res) {
|
|
|
|
const errors = req.validationErrors();
|
|
|
|
const email = req.body.email.toLowerCase();
|
2015-03-21 04:42:02 +00:00
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
if (errors) {
|
|
|
|
req.flash('errors', errors);
|
|
|
|
return res.redirect('/forgot');
|
|
|
|
}
|
2015-03-21 04:42:02 +00:00
|
|
|
|
2015-08-16 16:54:34 +00:00
|
|
|
User.resetPassword({
|
|
|
|
email: email
|
|
|
|
}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
req.flash('errors', err);
|
|
|
|
return res.redirect('/forgot');
|
2015-06-03 23:19:23 +00:00
|
|
|
}
|
2015-08-16 16:54:34 +00:00
|
|
|
|
|
|
|
req.flash('info', {
|
|
|
|
msg: 'An e-mail has been sent to ' +
|
|
|
|
email +
|
|
|
|
' with further instructions.'
|
|
|
|
});
|
|
|
|
res.render('account/forgot');
|
2015-06-03 23:19:23 +00:00
|
|
|
});
|
|
|
|
}
|
2015-06-04 00:14:45 +00:00
|
|
|
|
2015-10-02 03:55:55 +00:00
|
|
|
/*
|
2015-06-04 00:14:45 +00:00
|
|
|
function updateUserStoryPictures(userId, picture, username, cb) {
|
2015-07-23 06:27:18 +00:00
|
|
|
Story.find({ 'author.userId': userId }, function(err, stories) {
|
2015-08-01 03:22:08 +00:00
|
|
|
if (err) { return cb(err); }
|
2015-06-04 00:14:45 +00:00
|
|
|
|
2015-08-01 03:22:08 +00:00
|
|
|
const tasks = [];
|
|
|
|
stories.forEach(function(story) {
|
2015-06-04 00:14:45 +00:00
|
|
|
story.author.picture = picture;
|
|
|
|
story.author.username = username;
|
2015-07-23 06:27:18 +00:00
|
|
|
tasks.push(function(cb) {
|
2015-06-04 00:14:45 +00:00
|
|
|
story.save(cb);
|
|
|
|
});
|
2015-08-01 03:22:08 +00:00
|
|
|
});
|
2015-07-23 06:27:18 +00:00
|
|
|
async.parallel(tasks, function(err) {
|
2015-06-04 00:14:45 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
});
|
2015-08-01 03:22:08 +00:00
|
|
|
});
|
2015-06-04 00:14:45 +00:00
|
|
|
}
|
2015-10-02 03:55:55 +00:00
|
|
|
*/
|
2015-09-27 17:49:44 +00:00
|
|
|
|
2015-10-02 03:55:55 +00:00
|
|
|
function vote1(req, res, next) {
|
2015-09-27 17:49:44 +00:00
|
|
|
if (req.user) {
|
|
|
|
req.user.tshirtVote = 1;
|
2015-10-02 03:55:55 +00:00
|
|
|
req.user.save(function(err) {
|
|
|
|
if (err) { return next(err); }
|
|
|
|
|
|
|
|
req.flash('success', { msg: 'Thanks for voting!' });
|
2015-09-27 17:49:44 +00:00
|
|
|
res.redirect('/map');
|
|
|
|
});
|
|
|
|
} else {
|
2015-10-02 03:55:55 +00:00
|
|
|
req.flash('error', { msg: 'You must be signed in to vote.' });
|
2015-09-27 17:49:44 +00:00
|
|
|
res.redirect('/map');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 03:55:55 +00:00
|
|
|
function vote2(req, res, next) {
|
2015-09-27 17:49:44 +00:00
|
|
|
if (req.user) {
|
|
|
|
req.user.tshirtVote = 2;
|
2015-10-02 03:55:55 +00:00
|
|
|
req.user.save(function(err) {
|
|
|
|
if (err) { return next(err); }
|
|
|
|
|
|
|
|
req.flash('success', { msg: 'Thanks for voting!' });
|
2015-09-27 17:49:44 +00:00
|
|
|
res.redirect('/map');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
req.flash('error', {msg: 'You must be signed in to vote.'});
|
|
|
|
res.redirect('/map');
|
|
|
|
}
|
|
|
|
}
|
2015-06-03 23:19:23 +00:00
|
|
|
};
|