2018-01-08 17:25:45 -05:00
|
|
|
export function ifNoUserRedirectTo(url, message, type = 'danger') {
|
2015-06-20 11:43:12 -07:00
|
|
|
return function(req, res, next) {
|
2015-10-06 00:13:51 -07:00
|
|
|
const { path } = req;
|
2015-06-20 11:43:12 -07:00
|
|
|
if (req.user) {
|
|
|
|
return next();
|
|
|
|
}
|
2015-10-06 00:13:51 -07:00
|
|
|
|
2015-12-29 11:43:06 -08:00
|
|
|
req.flash(type, {
|
2016-05-12 21:28:29 -07:00
|
|
|
msg: message || `You must be signed in to access ${path}`
|
2015-10-06 00:13:51 -07:00
|
|
|
});
|
|
|
|
|
2015-06-20 11:43:12 -07:00
|
|
|
return res.redirect(url);
|
|
|
|
};
|
2015-10-02 11:47:36 -07:00
|
|
|
}
|
2015-06-20 11:43:12 -07:00
|
|
|
|
2015-10-02 11:47:36 -07:00
|
|
|
export function ifNoUserSend(sendThis) {
|
2015-06-22 16:43:31 -07:00
|
|
|
return function(req, res, next) {
|
|
|
|
if (req.user) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return res.status(200).send(sendThis);
|
|
|
|
};
|
2015-10-02 11:47:36 -07:00
|
|
|
}
|
2015-08-18 19:48:42 -07:00
|
|
|
|
2015-10-02 11:47:36 -07:00
|
|
|
export function ifNoUser401(req, res, next) {
|
2015-08-18 19:48:42 -07:00
|
|
|
if (req.user) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return res.status(401).end();
|
2015-10-02 11:47:36 -07:00
|
|
|
}
|
2016-05-07 17:46:39 +05:30
|
|
|
|
2016-12-15 02:54:59 +05:30
|
|
|
export function ifNotVerifiedRedirectToSettings(req, res, next) {
|
|
|
|
const { user } = req;
|
2016-06-02 23:39:23 -07:00
|
|
|
if (!user) {
|
2016-05-07 17:46:39 +05:30
|
|
|
return next();
|
2016-06-02 23:39:23 -07:00
|
|
|
}
|
2016-12-15 02:54:59 +05:30
|
|
|
if (!user.emailVerified) {
|
2018-01-08 17:25:45 -05:00
|
|
|
req.flash('danger', {
|
2016-12-15 02:54:59 +05:30
|
|
|
msg: 'We do not have your verified email address on record, '
|
|
|
|
+ 'please add it in the settings to continue with your request.'
|
2016-06-02 23:39:23 -07:00
|
|
|
});
|
2016-12-15 02:54:59 +05:30
|
|
|
return res.redirect('/settings');
|
2016-06-02 23:39:23 -07:00
|
|
|
}
|
2016-12-15 02:54:59 +05:30
|
|
|
return next();
|
2016-05-07 17:46:39 +05:30
|
|
|
}
|
2017-12-27 10:11:17 -08:00
|
|
|
|
|
|
|
export function ifUserRedirectTo(path = '/', status) {
|
|
|
|
status = status === 302 ? 302 : 301;
|
|
|
|
return (req, res, next) => {
|
|
|
|
if (req.user) {
|
|
|
|
return res.status(status).redirect(path);
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
};
|
|
|
|
}
|