2016-05-07 17:46:39 +05:30
|
|
|
import dedent from 'dedent';
|
|
|
|
|
2015-12-29 11:43:06 -08:00
|
|
|
export function ifNoUserRedirectTo(url, message, type = 'errors') {
|
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
|
|
|
|
|
|
|
export function flashIfNotVerified(req, res, next) {
|
|
|
|
const user = req.user;
|
|
|
|
if (!user) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
const email = req.user.email;
|
|
|
|
const emailVerified = req.user.emailVerified;
|
|
|
|
if (!email) {
|
|
|
|
req.flash('info', { msg:
|
|
|
|
dedent `Please update your email address when you get a moment in
|
|
|
|
your <a href="\settings"> Settings Page.</a>`
|
|
|
|
});
|
|
|
|
} else if (!emailVerified) {
|
|
|
|
req.flash('info', { msg:
|
|
|
|
dedent `We have your email address with us, but its not yet verified.
|
|
|
|
Please follow the link we sent you, when you get a moment.`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
}
|