auth.js
822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 登录判断
* @author: TaoHuang
* @date: 2016/7/15
*/
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
module.exports = (req, res, next) => {
req.getUrl = function() {
return req.protocol + '://' + req.get('host') + req.originalUrl;
};
let refer = '';
if (req.method === 'GET') {
refer = req.getUrl();
} else {
refer = req.get('Referer');
}
let loginUrl = helpers.urlFormat('/passport/login', {refer: refer});
if (_.isEmpty(req.user) || !req.user.uid) {
if (req.xhr) {
return res.json({
code: 400,
message: '抱歉,您暂未登录!',
data: {refer: loginUrl}
});
}
return res.redirect(loginUrl);
}
next();
};