fix(server/flash): Api to match documentation

This fixes duplication issues and normalize our use with everyone else
pull/16408/head
Berkeley Martinez 2018-01-12 14:16:33 -08:00
parent 0cd21f930f
commit 4a911931d0
10 changed files with 88 additions and 86 deletions

View File

@ -171,9 +171,10 @@ module.exports = function enableAuthentication(app) {
redirectTo = req.session.returnTo;
}
req.flash('success', { msg:
req.flash(
'success',
'Success! You have signed in to your account. Happy Coding!'
});
);
return res.redirect(redirectTo);
})

View File

@ -261,10 +261,10 @@ export default function(app) {
!completedChallenge.githubLink
)
) {
req.flash('danger', {
msg: 'You haven\'t supplied the necessary URLs for us to inspect ' +
'your work.'
});
req.flash(
'danger',
'You haven\'t supplied the necessary URLs for us to inspect your work.'
);
return res.sendStatus(403);
}

View File

@ -103,14 +103,15 @@ export default function commit(app) {
pledge => {
if (pledge) {
debug('found previous pledge');
req.flash('info', {
msg: dedent`
req.flash(
'info',
dedent`
Looks like you already have a pledge to ${pledge.displayName}.
Clicking "Commit" here will replace your old commitment. If you
do change your commitment, please remember to cancel your
previous recurring donation directly with ${pledge.displayName}.
`
});
);
}
res.render(
'commit/',
@ -165,14 +166,15 @@ export default function commit(app) {
})
.subscribe(
({ displayName, goal, amount }) => {
req.flash('success', {
msg: dedent`
req.flash(
'success',
dedent`
Congratulations, you have committed to giving
${displayName} $${amount} each month until you have completed
your ${goal}. Please remember to cancel your pledge directly
with ${displayName} once you finish.
`
});
);
res.redirect('/' + user.username);
},
next
@ -229,7 +231,7 @@ export default function commit(app) {
there's no pledge to stop.
`;
}
req.flash('info', { msg });
req.flash('info', msg);
return res.redirect(`/${user.username}`);
},
next

View File

@ -134,15 +134,17 @@ module.exports = function(app) {
return User.updateAll(query, params, function(err, info) {
if (err) { return next(err); }
if (info.count === 0) {
req.flash('info', {
msg: 'Email address not found. ' +
req.flash(
'info',
'Email address not found. ' +
'Please update your Email preferences from your profile.'
});
);
return res.redirect('/map');
} else {
req.flash('info', {
msg: 'We\'ve successfully updated your Email preferences.'
});
req.flash(
'info',
'We\'ve successfully updated your Email preferences.'
);
return res.redirect('/unsubscribed');
}
});

View File

@ -266,25 +266,19 @@ module.exports = function(app) {
let social = req.params.social;
if (!social) {
req.flash('danger', {
msg: 'No social account found'
});
req.flash('danger', 'No social account found');
return res.redirect('/' + username);
}
social = social.toLowerCase();
const validSocialAccounts = ['twitter', 'linkedin'];
if (validSocialAccounts.indexOf(social) === -1) {
req.flash('danger', {
msg: 'Invalid social account'
});
req.flash('danger', 'Invalid social account');
return res.redirect('/' + username);
}
if (!user[social]) {
req.flash('danger', {
msg: `No ${social} account associated`
});
req.flash('danger', `No ${social} account associated`);
return res.redirect('/' + username);
}
@ -300,9 +294,7 @@ module.exports = function(app) {
// assumed user identity is unique by provider
let identity = identities.shift();
if (!identity) {
req.flash('danger', {
msg: 'No social account found'
});
req.flash('danger', 'No social account found');
return res.redirect('/' + username);
}
@ -315,9 +307,7 @@ module.exports = function(app) {
.subscribe(() => {
debug(`${social} has been unlinked successfully`);
req.flash('info', {
msg: `You\'ve successfully unlinked your ${social}.`
});
req.flash('info', `You've successfully unlinked your ${social}.`);
return res.redirect('/' + username);
}, next);
});
@ -380,13 +370,14 @@ module.exports = function(app) {
}, {});
if (userPortfolio.isCheater && !user) {
req.flash('danger', {
msg: dedent`
req.flash(
'danger',
dedent`
Upon review, this account has been flagged for academic
dishonesty. If youre the owner of this account contact
team@freecodecamp.org for details.
`
});
);
}
if (userPortfolio.bio) {
@ -444,18 +435,20 @@ module.exports = function(app) {
.subscribe(
user => {
if (!user) {
req.flash('danger', {
msg: `We couldn't find a user with the username ${username}`
});
req.flash(
'danger',
`We couldn't find a user with the username ${username}`
);
return res.redirect('/');
}
if (!user.isGithubCool) {
req.flash('danger', {
msg: dedent`
req.flash(
'danger',
dedent`
This user needs to link GitHub with their account
in order for others to be able to view their certificate.
`
});
);
return res.redirect('back');
}
@ -464,21 +457,23 @@ module.exports = function(app) {
}
if (user.isLocked) {
req.flash('danger', {
msg: dedent`
req.flash(
'danger',
dedent`
${username} has chosen to make their profile
private. They will need to make their profile public
in order for others to be able to view their certificate.
`
});
);
return res.redirect('back');
}
if (!user.isHonest) {
req.flash('danger', {
msg: dedent`
req.flash(
'danger',
dedent`
${username} has not yet agreed to our Academic Honesty Pledge.
`
});
);
return res.redirect('back');
}
@ -496,9 +491,10 @@ module.exports = function(app) {
}
);
}
req.flash('danger', {
msg: `Looks like user ${username} is not ${certText[certType]}`
});
req.flash(
'danger',
`Looks like user ${username} is not ${certText[certType]}`
);
return res.redirect('back');
},
next
@ -513,7 +509,7 @@ module.exports = function(app) {
User.destroyById(req.user.id, function(err) {
if (err) { return next(err); }
req.logout();
req.flash('info', { msg: 'You\'ve successfully deleted your account.' });
req.flash('info', 'You\'ve successfully deleted your account.');
return res.redirect('/');
});
}
@ -541,7 +537,7 @@ module.exports = function(app) {
challegesCompleted: []
}, function(err) {
if (err) { return next(err); }
req.flash('info', { msg: 'You\'ve successfully reset your progress.' });
req.flash('info', 'You\'ve successfully reset your progress.');
return res.redirect('/');
});
});
@ -561,9 +557,10 @@ module.exports = function(app) {
const report = req.sanitize('reportDescription').trimTags();
if (!username || !report || report === '') {
req.flash('danger', {
msg: 'Oops, something is not right please re-check your submission.'
});
req.flash(
'danger',
'Oops, something is not right please re-check your submission.'
);
return next();
}
@ -591,9 +588,10 @@ module.exports = function(app) {
return next(err);
}
req.flash('info', {
msg: 'A report was sent to the team with ' + user.email + ' in copy.'
});
req.flash(
'info',
`A report was sent to the team with ${user.email} in copy.`
);
return res.redirect('/');
});
}

View File

@ -23,9 +23,7 @@ export default function redirectLang(app) {
}
if (type === 'html') {
req.flash('danger', {
msg: `We couldn't find path ${ path }`
});
req.flash('danger', `We couldn't find path ${ path }`);
return res.render('404', { title: '404'});
}

View File

@ -79,10 +79,7 @@ export default function prodErrorHandler() {
);
}
if (typeof req.flash === 'function') {
req.flash(
handled.type || 'danger',
{ msg: message }
);
req.flash(handled.type || 'danger', message);
}
return res.redirect(redirectTo);
// json

View File

@ -16,13 +16,14 @@ export default function flashCheaters() {
EXCLUDED_PATHS.indexOf(req.path) === -1 &&
req.user && req.url !== '/' && req.user.isCheater
) {
req.flash('danger', {
msg: dedent`
req.flash(
'danger',
dedent`
Upon review, this account has been flagged for academic
dishonesty. If youre the owner of this account contact
team@freecodecamp.org for details.
`
});
);
}
return next();
};

View File

@ -1,13 +1,13 @@
export function ifNoUserRedirectTo(url, message, type = 'danger') {
import dedent from 'dedent';
export function ifNoUserRedirectTo(url, message, type = 'errors') {
return function(req, res, next) {
const { path } = req;
if (req.user) {
return next();
}
req.flash(type, {
msg: message || `You must be signed in to access ${path}`
});
req.flash(type, message || `You must be signed in to access ${path}`);
return res.redirect(url);
};
@ -35,10 +35,13 @@ export function ifNotVerifiedRedirectToSettings(req, res, next) {
return next();
}
if (!user.emailVerified) {
req.flash('danger', {
msg: 'We do not have your verified email address on record, '
+ 'please add it in the settings to continue with your request.'
});
req.flash(
'danger',
dedent`
We do not have your verified email address on record,
please add it in the settings to continue with your request.
`
);
return res.redirect('/settings');
}
return next();

View File

@ -1,21 +1,21 @@
.container
.row.flashMessage
.col-xs-12.col-sm-8.col-sm-offset-2.col-md-6.col-md-offset-3
if (messages.danger)
if messages.danger
.alert.alert-danger.fade.in
button.close(type='button', data-dismiss='alert')
span.ion-close-circled
for danger in (messages.danger)
div!= danger.msg || danger
for msg in messages.danger
div!= msg
if messages.info
.alert.alert-info.fade.in
button.close(type='button', data-dismiss='alert')
span.ion-close-circled
for info in messages.info
div!= info.msg
for msg in messages.info
div!= msg
if messages.success
.alert.alert-success.fade.in
button.close(type='button', data-dismiss='alert')
span.ion-close-circled
for success in messages.success
div!= success.msg
for msg in messages.success
div!= msg