captcha.js 809 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.status(510).json({
            data: {refer: limitAPI}
        });
    }

    return res.redirect(limitPage);
};