captcha.js 820 Bytes
'use strict';

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

const WHITE_LIST = [
    '/3party/check',
    '/passport/imagesNode',
    '/passport/images.png',
    '/passport/cert/headerTip'
];

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 (res.statusCode === 403) {
        return res.end();
    }

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

    return res.redirect(limitPage);
};