2018-01-12 22:16:33 +00:00
|
|
|
import dedent from 'dedent';
|
2018-01-23 01:08:33 +00:00
|
|
|
import { validationResult } from 'express-validator/check';
|
|
|
|
|
|
|
|
import { createValidatorErrorFormatter } from './create-handled-error.js';
|
2018-10-23 23:24:48 +00:00
|
|
|
import { homeLocation } from '../../../config/env.json';
|
2018-01-12 22:16:33 +00:00
|
|
|
|
|
|
|
export function ifNoUserRedirectTo(url, message, type = 'errors') {
|
2015-06-20 18:43:12 +00:00
|
|
|
return function(req, res, next) {
|
2015-10-06 07:13:51 +00:00
|
|
|
const { path } = req;
|
2015-06-20 18:43:12 +00:00
|
|
|
if (req.user) {
|
|
|
|
return next();
|
|
|
|
}
|
2015-10-06 07:13:51 +00:00
|
|
|
|
2018-01-12 22:16:33 +00:00
|
|
|
req.flash(type, message || `You must be signed in to access ${path}`);
|
2015-10-06 07:13:51 +00:00
|
|
|
|
2015-06-20 18:43:12 +00:00
|
|
|
return res.redirect(url);
|
|
|
|
};
|
2015-10-02 18:47:36 +00:00
|
|
|
}
|
2015-06-20 18:43:12 +00:00
|
|
|
|
2015-10-02 18:47:36 +00:00
|
|
|
export function ifNoUserSend(sendThis) {
|
2015-06-22 23:43:31 +00:00
|
|
|
return function(req, res, next) {
|
|
|
|
if (req.user) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return res.status(200).send(sendThis);
|
|
|
|
};
|
2015-10-02 18:47:36 +00:00
|
|
|
}
|
2015-08-19 02:48:42 +00:00
|
|
|
|
2015-10-02 18:47:36 +00:00
|
|
|
export function ifNoUser401(req, res, next) {
|
2015-08-19 02:48:42 +00:00
|
|
|
if (req.user) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return res.status(401).end();
|
2015-10-02 18:47:36 +00:00
|
|
|
}
|
2016-05-07 12:16:39 +00:00
|
|
|
|
2018-05-25 17:44:09 +00:00
|
|
|
export function ifNotVerifiedRedirectToUpdateEmail(req, res, next) {
|
2016-12-14 21:24:59 +00:00
|
|
|
const { user } = req;
|
2016-06-03 06:39:23 +00:00
|
|
|
if (!user) {
|
2016-05-07 12:16:39 +00:00
|
|
|
return next();
|
2016-06-03 06:39:23 +00:00
|
|
|
}
|
2016-12-14 21:24:59 +00:00
|
|
|
if (!user.emailVerified) {
|
2018-01-12 22:16:33 +00:00
|
|
|
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.
|
|
|
|
`
|
|
|
|
);
|
2016-12-14 21:24:59 +00:00
|
|
|
return res.redirect('/settings');
|
2016-06-03 06:39:23 +00:00
|
|
|
}
|
2016-12-14 21:24:59 +00:00
|
|
|
return next();
|
2016-05-07 12:16:39 +00:00
|
|
|
}
|
2017-12-27 18:11:17 +00:00
|
|
|
|
2018-10-23 23:24:48 +00:00
|
|
|
export function ifUserRedirectTo(path = `${homeLocation}/welcome`, status) {
|
2017-12-27 18:11:17 +00:00
|
|
|
status = status === 302 ? 302 : 301;
|
|
|
|
return (req, res, next) => {
|
|
|
|
if (req.user) {
|
|
|
|
return res.status(status).redirect(path);
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
};
|
|
|
|
}
|
2018-01-23 01:08:33 +00:00
|
|
|
|
|
|
|
// for use with express-validator error formatter
|
|
|
|
export const createValidatorErrorHandler = (...args) => (req, res, next) => {
|
2018-10-23 23:24:48 +00:00
|
|
|
const validation = validationResult(req).formatWith(
|
|
|
|
createValidatorErrorFormatter(...args)
|
|
|
|
);
|
2018-01-23 01:08:33 +00:00
|
|
|
|
|
|
|
if (!validation.isEmpty()) {
|
|
|
|
const errors = validation.array();
|
|
|
|
return next(errors.pop());
|
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
};
|