freeCodeCamp/server/boot/user.js

516 lines
14 KiB
JavaScript
Raw Normal View History

2015-10-02 18:47:36 +00:00
import _ from 'lodash';
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';
import {
2015-12-09 20:28:19 +00:00
frontEndChallengeId,
2016-01-11 23:58:37 +00:00
dataVisChallengeId,
2015-12-09 20:28:19 +00:00
backEndChallengeId
} from '../utils/constantStrings.json';
2016-01-11 23:58:37 +00:00
import certTypes from '../utils/certTypes.json';
import { ifNoUser401, ifNoUserRedirectTo } from '../utils/middleware';
2015-10-02 18:47:36 +00:00
import { observeQuery } from '../utils/rx';
import { calcCurrentStreak, calcLongestStreak } from '../utils/user-stats';
2015-08-20 16:40:03 +00:00
const debug = debugFactory('freecc:boot:user');
const sendNonUserToMap = ifNoUserRedirectTo('/map');
2016-01-11 23:58:37 +00:00
const certIds = {
[certTypes.frontEnd]: frontEndChallengeId,
[certTypes.dataVis]: dataVisChallengeId,
[certTypes.backEnd]: backEndChallengeId
};
const certViews = {
[certTypes.frontEnd]: 'certificate/front-end.jade',
[certTypes.dataVis]: 'certificate/data-vis.jade',
[certTypes.backEnd]: 'certificate/back-end.jade',
[certTypes.fullStack]: 'certificate/full-stack.jade'
};
const certText = {
[certTypes.fronEnd]: 'Front End certified',
[certTypes.dataVis]: 'Data Vis Certified',
[certTypes.backEnd]: 'Back End Certified',
[certTypes.fullStack]: 'Full Stack Certified'
};
function replaceScriptTags(value) {
return value
.replace(/<script>/gi, 'fccss')
.replace(/<\/script>/gi, 'fcces');
}
function replaceFormAction(value) {
return value.replace(/<form[^>]*>/, function(val) {
return val.replace(/action(\s*?)=/, 'fccfaa$1=');
});
}
function encodeFcc(value = '') {
return replaceScriptTags(replaceFormAction(value));
}
module.exports = function(app) {
var router = app.loopback.Router();
var User = app.models.User;
2015-10-02 18:47:36 +00:00
function findUserByUsername$(username, fields) {
return observeQuery(
User,
'findOne',
{
where: { username },
fields
}
);
}
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);
router.get('/email-signup', getEmailSignup);
router.get('/email-signin', getEmailSignin);
router.get(
'/toggle-lockdown-mode',
sendNonUserToMap,
toggleLockdownMode
);
2015-08-20 16:40:03 +00:00
router.post(
'/account/delete',
ifNoUser401,
postDeleteAccount
);
router.get(
'/account',
sendNonUserToMap,
getAccount
);
2015-09-27 17:49:44 +00:00
router.get('/vote1', vote1);
router.get('/vote2', vote2);
2015-10-02 18:47:36 +00:00
// Ensure these are the last routes!
router.get(
'/:username/front-end-certification',
2016-01-11 23:58:37 +00:00
showCert.bind(null, certTypes.frontEnd)
2015-10-02 18:47:36 +00:00
);
router.get(
2016-01-11 23:58:37 +00:00
'/:username/data-visualization-certification',
showCert.bind(null, certTypes.dataVis)
2015-12-09 20:28:19 +00:00
);
router.get(
'/:username/back-end-certification',
2016-01-11 23:58:37 +00:00
showCert.bind(null, certTypes.backEnd)
);
router.get(
'/:username/full-stack-certification',
(req, res) => res.redirect(req.url.replace('full-stack', 'back-end'))
2015-10-02 18:47:36 +00:00
);
router.get('/:username', returnUser);
app.use(router);
2015-07-23 06:27:18 +00:00
function getSignin(req, res) {
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'
});
}
2014-01-13 09:34:54 +00:00
2015-07-23 06:27:18 +00:00
function signout(req, res) {
req.logout();
res.redirect('/');
}
2015-07-23 06:27:18 +00:00
function getEmailSignin(req, res) {
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-07-23 06:27:18 +00:00
function getEmailSignup(req, res) {
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-07-23 06:27:18 +00:00
function getAccount(req, res) {
const { username } = req.user;
return res.redirect('/' + username);
}
2015-07-23 06:27:18 +00:00
function returnUser(req, res, next) {
const username = req.params.username.toLowerCase();
const { path } = req;
User.findOne(
{
where: { username },
include: 'pledge'
},
function(err, profileUser) {
if (err) {
return next(err);
}
if (!profileUser) {
req.flash('errors', {
msg: `404: We couldn't find path ${ path }`
});
2016-01-15 08:10:13 +00:00
console.log('404');
return res.redirect('/');
}
profileUser = profileUser.toJSON();
var cals = profileUser
.progressTimestamps
.map(objOrNum => {
return typeof objOrNum === 'number' ?
objOrNum :
objOrNum.timestamp;
})
.sort();
profileUser.currentStreak = calcCurrentStreak(cals);
profileUser.longestStreak = calcLongestStreak(cals);
const data = profileUser
.progressTimestamps
.map((objOrNum) => {
return typeof objOrNum === 'number' ?
objOrNum :
objOrNum.timestamp;
})
.filter((timestamp) => {
return !!timestamp;
})
.reduce((data, timeStamp) => {
data[(timeStamp / 1000)] = 1;
return data;
}, {});
const baseAndZip = profileUser.completedChallenges.filter(
function(obj) {
2015-08-05 20:21:53 +00:00
return obj.challengeType === 3 || obj.challengeType === 4;
}
);
const bonfires = profileUser.completedChallenges.filter(function(obj) {
2015-12-08 21:00:03 +00:00
return (obj.name || '').match(/^Bonfire/g);
});
const waypoints = profileUser.completedChallenges.filter(function(obj) {
return (obj.name || '').match(/^Waypoint|^Checkpoint/i);
});
res.render('account/show', {
2015-10-31 09:59:09 +00:00
title: 'Camper ' + profileUser.username + '\'s Code Portfolio',
username: profileUser.username,
name: profileUser.name,
2015-10-02 18:47:36 +00:00
isMigrationGrandfathered: profileUser.isMigrationGrandfathered,
isGithubCool: profileUser.isGithubCool,
isLocked: !!profileUser.isLocked,
pledge: profileUser.pledge,
2015-10-02 18:47:36 +00:00
isFrontEndCert: profileUser.isFrontEndCert,
2016-01-11 23:58:37 +00:00
isDataVisCert: profileUser.isDataVisCert,
2015-12-09 20:28:19 +00:00
isBackEndCert: profileUser.isBackEndCert,
2015-10-02 18:47:36 +00:00
isFullStackCert: profileUser.isFullStackCert,
isHonest: profileUser.isHonest,
location: profileUser.location,
calender: data,
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,
encodeFcc
});
}
);
}
2016-01-11 23:58:37 +00:00
function showCert(certType, req, res, next) {
2015-10-02 18:47:36 +00:00
const username = req.params.username.toLowerCase();
const { user } = req;
Observable.just(user)
.flatMap(user => {
if (user && user.username === username) {
return Observable.just(user);
}
return findUserByUsername$(username, {
isGithubCool: true,
2015-10-02 18:47:36 +00:00
isFrontEndCert: true,
2016-01-11 23:58:37 +00:00
isDataVisCert: true,
2015-12-09 20:28:19 +00:00
isBackEndCert: true,
2016-01-11 23:58:37 +00:00
isFullStackCert: true,
isHonest: true,
2015-10-02 18:47:36 +00:00
completedChallenges: true,
username: true,
name: true
});
})
.subscribe(
(user) => {
if (!user) {
req.flash('errors', {
2016-01-14 23:15:44 +00:00
msg: `We couldn't find the user with the username ${username}`
2015-10-02 18:47:36 +00:00
});
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
2016-01-04 08:01:38 +00:00
in order for others to be able to view their certificate.
2015-10-06 05:42:42 +00:00
`
});
return res.redirect('back');
}
if (user.isLocked) {
req.flash('errors', {
msg: dedent`
2016-01-11 01:41:50 +00:00
${username} has chosen to make their profile
private. They will need to make their profile public
2016-01-04 08:01:38 +00:00
in order for others to be able to view their certificate.
`
});
2015-10-06 05:42:42 +00:00
return res.redirect('back');
}
if (!user.isHonest) {
req.flash('errors', {
msg: dedent`
2016-01-04 08:01:38 +00:00
${username} has not yet agreed to our Academic Honesty Pledge.
`
});
2015-10-06 05:42:42 +00:00
return res.redirect('back');
}
2016-01-11 23:58:37 +00:00
if (user[certType]) {
// find challenge in user profile
// if not found supply empty object
// if found grab date
// if no date use todays date
2015-10-08 15:48:52 +00:00
var { completedDate = new Date() } =
2016-01-11 23:58:37 +00:00
_.find(
user.completedChallenges,
{ id: certIds[certType] }
) || {};
2015-10-02 18:47:36 +00:00
return res.render(
2016-01-11 23:58:37 +00:00
certViews[certType],
2015-10-02 18:47:36 +00:00
{
username: user.username,
2016-01-11 23:58:37 +00:00
date: moment(new Date(completedDate)).format('MMMM, Do YYYY'),
2015-10-02 18:47:36 +00:00
name: user.name
}
);
}
req.flash('errors', {
2016-01-11 23:58:37 +00:00
msg: `Looks like user ${username} is not ${certText[certType]}`
2015-10-02 18:47:36 +00:00
});
2015-10-08 15:48:52 +00:00
res.redirect('back');
2015-10-02 18:47:36 +00:00
},
next
);
}
function toggleLockdownMode(req, res, next) {
if (req.user.isLocked === true) {
req.user.isLocked = false;
return req.user.save(function(err) {
if (err) { return next(err); }
2015-09-28 00:08:56 +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
});
res.redirect('/' + req.user.username);
});
2015-09-28 00:08:56 +00:00
}
req.user.isLocked = true;
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) {
User.destroyById(req.user.id, function(err) {
if (err) { return next(err); }
req.logout();
req.flash('info', { msg: 'Your account has been deleted.' });
res.redirect('/');
});
}
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-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-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;
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) {
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-07-23 06:27:18 +00:00
function getForgot(req, res) {
if (req.isAuthenticated()) {
return res.redirect('/');
}
res.render('account/forgot', {
title: 'Forgot Password'
});
}
2015-08-16 16:54:34 +00:00
function postForgot(req, res) {
const errors = req.validationErrors();
const email = req.body.email.toLowerCase();
if (errors) {
req.flash('errors', errors);
return res.redirect('/forgot');
}
2015-08-16 16:54:34 +00:00
User.resetPassword({
email: email
}, function(err) {
if (err) {
req.flash('errors', err.message);
2015-08-16 16:54:34 +00:00
return res.redirect('/forgot');
}
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-04 00:14:45 +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) {
if (err) { return cb(err); }
2015-06-04 00:14:45 +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-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-06-04 00:14:45 +00:00
}
*/
2015-09-27 17:49:44 +00:00
function vote1(req, res, next) {
2015-09-27 17:49:44 +00:00
if (req.user) {
req.user.tshirtVote = 1;
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.' });
2015-09-27 17:49:44 +00:00
res.redirect('/map');
}
}
function vote2(req, res, next) {
2015-09-27 17:49:44 +00:00
if (req.user) {
req.user.tshirtVote = 2;
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');
}
}
};