auth.js 781 Bytes
var urlFormat = require('../util/url');

/**
 * 登录判断
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/4/5
 */

module.exports = (req, res, next) => {
    var loginUrl = 'http://www.yohobuy.com/signin.html',
        referrer = urlFormat(req.originalUrl || '/', null, 'activity');

    if (req.isAPP && !req.app.uid) {
        return res.redirect(loginUrl + '?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' + referrer + '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}'); // 跳转APP登录页
    }

    if (!req.cookies._UID) {
        loginUrl = loginUrl + '?refer=' + encodeURIComponent(referrer);
        return req.xhr ? res.json({loginUrl: loginUrl}) : res.redirect(loginUrl); // 跳转登录页
    }

    next();
};