auth.js 704 Bytes
/**
 * 登录判断
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/4/25
 */
'use strict';

const helpers = global.yoho.helpers;

module.exports = (req, res, next) => {
    if (!req.user.uid) {
        if (req.xhr) {
            return res.json({
                code: 400,
                message: '抱歉,您暂未登录!',
                redirect: '/passport/login'
            });
        } else if (req.yoho.isApp) {
            return next({
                code: 401,
                message: 'weblogin'
            });
        } else {
            return res.redirect(helpers.urlFormat('/signin.html', {
                refer: req.originalUrl
            }));
        }
    }

    next();
};