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

'use strict';

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

module.exports = (req, res, next) => {
    let refer = req.method === 'GET' ? req.get('Referer') : '';

    let loginAPI = helpers.urlFormat('/passport/login', {refer: refer});

    let loginPage = helpers.urlFormat('/passport/login', {refer: req.fullUrl()});

    if (_.isEmpty(req.user) || !req.user.uid) {
        if (req.xhr) {
            return res.json({
                code: 400,
                message: '抱歉,您暂未登录!',
                data: {refer: loginAPI}
            });
        }

        return res.redirect(loginPage);
    }
    next();
};