2018-01-12 14:16:33 -08:00
|
|
|
import dedent from 'dedent';
|
2020-12-03 16:47:38 +01:00
|
|
|
import { validationResult } from 'express-validator';
|
2018-01-22 17:08:33 -08:00
|
|
|
|
|
|
|
import { createValidatorErrorFormatter } from './create-handled-error.js';
|
2021-02-26 01:32:35 +05:30
|
|
|
|
2019-03-04 21:14:41 +00:00
|
|
|
import {
|
|
|
|
getAccessTokenFromRequest,
|
|
|
|
removeCookies
|
|
|
|
} from './getSetAccessToken.js';
|
2020-12-30 20:10:38 +01:00
|
|
|
import { getRedirectParams } from './redirection';
|
2018-01-12 14:16:33 -08:00
|
|
|
|
2020-12-30 20:10:38 +01:00
|
|
|
export function ifNoUserRedirectHome(message, type = 'errors') {
|
2021-03-11 00:31:46 +05:30
|
|
|
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
|
|
|
|
2020-12-30 20:10:38 +01:00
|
|
|
const { origin } = getRedirectParams(req);
|
2018-01-12 14:16:33 -08:00
|
|
|
req.flash(type, message || `You must be signed in to access ${path}`);
|
2015-10-06 00:13:51 -07:00
|
|
|
|
2020-12-30 20:10:38 +01:00
|
|
|
return res.redirect(origin);
|
2015-06-20 11:43:12 -07:00
|
|
|
};
|
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) {
|
2021-03-11 00:31:46 +05:30
|
|
|
return function (req, res, next) {
|
2015-06-22 16:43:31 -07:00
|
|
|
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
|
|
|
|
2018-05-25 23:14:09 +05:30
|
|
|
export function ifNotVerifiedRedirectToUpdateEmail(req, res, next) {
|
2016-12-15 02:54:59 +05:30
|
|
|
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-12 14:16:33 -08: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-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
|
|
|
|
2020-12-30 20:10:38 +01:00
|
|
|
export function ifUserRedirectTo(status) {
|
2019-10-21 17:03:00 +05:30
|
|
|
status = status === 301 ? 301 : 302;
|
2017-12-27 10:11:17 -08:00
|
|
|
return (req, res, next) => {
|
2019-03-04 21:14:41 +00:00
|
|
|
const { accessToken } = getAccessTokenFromRequest(req);
|
2020-12-30 20:10:38 +01:00
|
|
|
const { returnTo } = getRedirectParams(req);
|
2019-03-04 21:14:41 +00:00
|
|
|
if (req.user && accessToken) {
|
2020-12-30 20:10:38 +01:00
|
|
|
return res.status(status).redirect(returnTo);
|
2017-12-27 10:11:17 -08:00
|
|
|
}
|
2019-03-04 21:14:41 +00:00
|
|
|
if (req.user && !accessToken) {
|
|
|
|
// This request has an active auth session
|
|
|
|
// but there is no accessToken attached to the request
|
|
|
|
// perhaps the user cleared cookies?
|
|
|
|
// we need to remove the zombie auth session
|
|
|
|
removeCookies(req, res);
|
|
|
|
delete req.session.passport;
|
|
|
|
}
|
2017-12-27 10:11:17 -08:00
|
|
|
return next();
|
|
|
|
};
|
|
|
|
}
|
2018-01-22 17:08:33 -08:00
|
|
|
|
|
|
|
// for use with express-validator error formatter
|
|
|
|
export const createValidatorErrorHandler = (...args) => (req, res, next) => {
|
2018-10-24 00:24:48 +01:00
|
|
|
const validation = validationResult(req).formatWith(
|
|
|
|
createValidatorErrorFormatter(...args)
|
|
|
|
);
|
2018-01-22 17:08:33 -08:00
|
|
|
|
|
|
|
if (!validation.isEmpty()) {
|
|
|
|
const errors = validation.array();
|
|
|
|
return next(errors.pop());
|
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
};
|