Blame view

doraemon/middleware/auth.js 745 Bytes
ccbikai authored
1 2 3 4 5 6 7
/**
 * 登录判断
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/4/25
 */
'use strict';
yyq authored
8 9
const helpers = global.yoho.helpers;
ccbikai authored
10
module.exports = (req, res, next) => {
yyq authored
11 12 13 14
    if (!req.user.uid) {
        if (req.xhr) {
            return res.json({
                code: 400,
陈轩 authored
15
                message: '抱歉,您暂未登录!',
郭成尧 authored
16
                redirect: '/signin.html'
yyq authored
17
            });
陈峰 authored
18 19 20 21 22 23 24
        } else if (req.yoho.isApp) {
            return next({
                code: 401,
                message: 'weblogin'
            });
        } else {
            return res.redirect(helpers.urlFormat('/signin.html', {
郭成尧 authored
25 26
                refer: req.originalUrl,
                from: req.query.from || ''
陈峰 authored
27
            }));
yyq authored
28 29 30
        }
    }
ccbikai authored
31 32
    next();
};