captcha.js 950 Bytes
'use strict';

const helpers = global.yoho.helpers;
const _ = require('lodash');

const WHITE_LIST = [
    '/3party/check',
    '/3party/check/submit',
    '/passport/imagesNode',
    '/passport/cert/headerTip',
    '/passport/captcha/get',
    '/passport/images',
    '/passport/img-check.jpg',
    '/passport/geetest/register'
];

module.exports = (req, res, next) => {
    let refer = req.method === 'GET' ? req.get('Referer') : '';
    let limitAPI = helpers.urlFormat('/3party/check', {refer: refer});
    let limitPage = helpers.urlFormat('/3party/check', {refer: req.protocol + '://' + req.get('host') + req.originalUrl});

    if (_.indexOf(WHITE_LIST, req.path) >= 0) {
        return next();
    }

    if (_.toNumber(res.statusCode) === 403) {
        return res.end();
    }

    if (req.xhr) {
        return res.json({
            code: 400,
            data: {refer: limitAPI}
        });
    }

    return res.redirect(limitPage);
};