2015-10-02 18:47:36 +00:00
|
|
|
import _ from 'lodash';
|
2015-10-02 03:55:55 +00:00
|
|
|
import dedent from 'dedent';
|
2015-08-20 16:40:03 +00:00
|
|
|
import moment from 'moment';
|
2015-10-02 18:47:36 +00:00
|
|
|
import { Observable } from 'rx';
|
2015-08-20 16:40:03 +00:00
|
|
|
import debugFactory from 'debug';
|
2015-08-07 20:31:48 +00:00
|
|
|
|
2015-10-06 03:00:25 +00:00
|
|
|
import {
|
|
|
|
frontEndChallangeId,
|
|
|
|
fullStackChallangeId
|
|
|
|
} from '../utils/constantStrings.json';
|
2015-10-02 03:55:55 +00:00
|
|
|
import { ifNoUser401, ifNoUserRedirectTo } from '../utils/middleware';
|
2015-10-02 18:47:36 +00:00
|
|
|
import { observeQuery } from '../utils/rx';
|
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-10-06 23:39:15 +00:00
|
|
|
function replaceScriptTags(value) {
|
|
|
|
return value
|
|
|
|
.replace(/<script>/gi, 'fccss')
|
|
|
|
.replace(/<\/script>/gi, 'fcces');
|
|
|
|
}
|
|
|
|
|
2015-12-08 19:30:47 +00:00
|
|
|
function replaceFormAction(value) {
|
|
|
|
return value.replace(/<form[^>]*>/, function(val) {
|
|
|
|
return val.replace(/action(\s*?)=/, 'fccfaa$1=');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function encodeFcc(value = '') {
|
|
|
|
return replaceScriptTags(replaceFormAction(value));
|
|
|
|
}
|
|
|
|
|
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 18:47:36 +00:00
|
|
|
function findUserByUsername$(username, fields) {
|
|
|
|
return observeQuery(
|
|
|
|
User,
|
|
|
|
'findOne',
|
|
|
|
{
|
|
|
|
where: { username },
|
|
|
|
fields
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
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-10-02 18:47:36 +00:00
|
|
|
|
|
|
|
// Ensure these are the last routes!
|
|
|
|
router.get(
|
|
|
|
'/:username/front-end-certification',
|
|
|
|
showCert
|
|
|
|
);
|
|
|
|
|
|
|
|
router.get(
|
|
|
|
'/:username/full-stack-certification',
|
|
|
|
showCert
|
|
|
|
);
|
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
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', {
|
2015-10-31 09:59:09 +00:00
|
|
|
title: 'Sign in to Free Code Camp using a Social Media Account'
|
2015-06-03 23:19:23 +00:00
|
|
|
});
|
|
|
|
}
|
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', {
|
2015-10-31 09:59:09 +00:00
|
|
|
title: 'Sign in to Free Code Camp using your Email Address'
|
2015-06-03 23:19:23 +00:00
|
|
|
});
|
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', {
|
2015-10-31 09:59:09 +00:00
|
|
|
title: 'Sign up for Free Code Camp using your Email Address'
|
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-10-06 21:54:59 +00:00
|
|
|
{
|
|
|
|
where: { username },
|
|
|
|
include: 'pledge'
|
|
|
|
},
|
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-06 21:54:59 +00:00
|
|
|
profileUser = profileUser.toJSON();
|
2015-08-05 20:01:19 +00:00
|
|
|
|
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-31 09:59:09 +00:00
|
|
|
title: 'Camper ' + profileUser.username + '\'s Code Portfolio',
|
2015-10-02 06:03:52 +00:00
|
|
|
username: profileUser.username,
|
|
|
|
name: profileUser.name,
|
2015-10-02 18:47:36 +00:00
|
|
|
|
2015-10-02 06:03:52 +00:00
|
|
|
isMigrationGrandfathered: profileUser.isMigrationGrandfathered,
|
|
|
|
isGithubCool: profileUser.isGithubCool,
|
|
|
|
isLocked: !!profileUser.isLocked,
|
|
|
|
|
2015-10-06 21:54:59 +00:00
|
|
|
pledge: profileUser.pledge,
|
|
|
|
|
2015-10-02 18:47:36 +00:00
|
|
|
isFrontEndCert: profileUser.isFrontEndCert,
|
|
|
|
isFullStackCert: profileUser.isFullStackCert,
|
|
|
|
isHonest: profileUser.isHonest,
|
|
|
|
|
2015-10-02 06:03:52 +00:00
|
|
|
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,
|
2015-10-06 23:39:15 +00:00
|
|
|
currentStreak: profileUser.currentStreak,
|
|
|
|
|
2015-12-08 19:30:47 +00:00
|
|
|
encodeFcc
|
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 18:47:36 +00:00
|
|
|
function showCert(req, res, next) {
|
|
|
|
const username = req.params.username.toLowerCase();
|
|
|
|
const { user } = req;
|
|
|
|
const showFront = req.path.split('/').pop() === 'front-end-certification';
|
|
|
|
Observable.just(user)
|
|
|
|
.flatMap(user => {
|
|
|
|
if (user && user.username === username) {
|
|
|
|
return Observable.just(user);
|
|
|
|
}
|
|
|
|
return findUserByUsername$(username, {
|
2015-10-08 16:02:35 +00:00
|
|
|
isGithubCool: true,
|
2015-10-02 18:47:36 +00:00
|
|
|
isFrontEndCert: true,
|
|
|
|
isFullStackCert: true,
|
2015-10-08 16:02:35 +00:00
|
|
|
isHonest: true,
|
2015-10-02 18:47:36 +00:00
|
|
|
completedChallenges: true,
|
|
|
|
username: true,
|
|
|
|
name: true
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.subscribe(
|
|
|
|
(user) => {
|
|
|
|
if (!user) {
|
|
|
|
req.flash('errors', {
|
|
|
|
msg: `404: We couldn't find the user ${username}`
|
|
|
|
});
|
|
|
|
return res.redirect('/');
|
|
|
|
}
|
2015-10-06 05:42:42 +00:00
|
|
|
if (!user.isGithubCool) {
|
|
|
|
req.flash('errors', {
|
|
|
|
msg: dedent`
|
|
|
|
This user needs to link GitHub with their account
|
|
|
|
in order to display this certificate to the public.
|
|
|
|
`
|
|
|
|
});
|
|
|
|
return res.redirect('back');
|
|
|
|
}
|
2015-10-06 03:00:25 +00:00
|
|
|
if (user.isLocked) {
|
|
|
|
req.flash('errors', {
|
|
|
|
msg: dedent`
|
2015-10-06 05:42:42 +00:00
|
|
|
${username} has chosen to hide their work from the public.
|
|
|
|
They need to unhide their work in order for this certificate to
|
|
|
|
be verifiable.
|
2015-10-06 03:00:25 +00:00
|
|
|
`
|
|
|
|
});
|
2015-10-06 05:42:42 +00:00
|
|
|
return res.redirect('back');
|
2015-10-06 03:00:25 +00:00
|
|
|
}
|
|
|
|
if (!user.isHonest) {
|
|
|
|
req.flash('errors', {
|
|
|
|
msg: dedent`
|
2015-10-06 05:42:42 +00:00
|
|
|
${username} has not agreed to our Academic Honesty Pledge yet.
|
2015-10-06 03:00:25 +00:00
|
|
|
`
|
|
|
|
});
|
2015-10-06 05:42:42 +00:00
|
|
|
return res.redirect('back');
|
2015-10-06 03:00:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 18:47:36 +00:00
|
|
|
if (
|
|
|
|
showFront && user.isFrontEndCert ||
|
|
|
|
!showFront && user.isFullStackCert
|
|
|
|
) {
|
2015-10-08 15:48:52 +00:00
|
|
|
var { completedDate = new Date() } =
|
|
|
|
_.find(user.completedChallenges, {
|
|
|
|
id: showFront ?
|
|
|
|
frontEndChallangeId :
|
|
|
|
fullStackChallangeId
|
|
|
|
}) || {};
|
2015-10-02 18:47:36 +00:00
|
|
|
|
|
|
|
return res.render(
|
|
|
|
showFront ?
|
|
|
|
'certificate/front-end.jade' :
|
|
|
|
'certificate/full-stack.jade',
|
|
|
|
{
|
|
|
|
username: user.username,
|
|
|
|
date: moment(new Date(completedDate))
|
|
|
|
.format('MMMM, Do YYYY'),
|
|
|
|
name: user.name
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
req.flash('errors', {
|
|
|
|
msg: showFront ?
|
|
|
|
`Looks like user ${username} is not Front End certified` :
|
|
|
|
`Looks like user ${username} is not Full Stack certified`
|
|
|
|
});
|
2015-10-08 15:48:52 +00:00
|
|
|
res.redirect('back');
|
2015-10-02 18:47:36 +00:00
|
|
|
},
|
|
|
|
next
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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', {
|
2015-10-31 09:59:09 +00:00
|
|
|
title: 'Reset your Password',
|
2015-08-16 16:54:34 +00:00
|
|
|
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-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
|
|
|
};
|