login.js
6.49 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'use strict';
const _ = require('lodash');
const passport = require('passport');
const uuid = require('uuid');
const helpers = global.yoho.helpers;
const log = global.yoho.logger;
const config = global.yoho.config;
const utils = global.yoho.utils;
const AuthHelper = require('../models/auth-helper');
const loginPage = `${config.siteUrl}/signin.html`;
function doPassportCallback(openId, nickname, headIcon, source, req, res) { // eslint-disable-line
if (openId && nickname && source) {
req.session._THIRD_LOGIN_INFO = {
openId,
nickname,
headIcon,
source
};
// 第三方登录
req.session._THIRD_LOGIN = true;
req.session._THIRD_LOGIN_SOURCE = source;
res.status(301).redirect(`${req.session.cbUrl}&auth_cb=true`);
} else {
return Promise.reject('missing third party login openId or nickname');
}
}
const common = {
beforeLogin: (req, res, next) => {
if (req.session.passwordWeak) {
return res.redirect('/passport/password/resetpage');
}
let refer = req.query.refer;
if (!refer) {
refer = req.get('Referer') || req.cookies.refer;
}
refer = utils.yoho.refererLimit(refer);
refer && !/signin|login|passport/.test(refer) && res.cookie('refer', encodeURI(refer), {
domain: 'yohobuy.com'
});
let from = req.query.from;
if (from) {
res.cookie('from', from, {
domain: 'yohobuy.com',
expires: new Date(Date.now() + 10 * 60 * 1000)
});
}
if (req.yoho.isApp) {
return next({
code: 401,
message: 'weblogin',
refer
});
}
next();
},
weixinCheck: (req, res, next) => {
let passLogin = _.get(req, 'cookies._WX_PASS_LOGIN', false);
if (req.yoho.isWechat && !passLogin) {
return res.redirect(helpers.urlFormat('/passport/login/wechat', {
refer: req.query.refer || req.get('Referer') || '/'
}));
}
next();
},
clearCookie: (req, res, next) => {
res.clearCookie('_SESSION_KEY', {
domain: 'yohobuy.com'
});
res.clearCookie('_UID', {
domain: 'yohobuy.com'
});
res.clearCookie('_TOKEN', {
domain: 'yohobuy.com'
});
if (req.session2 && req.session2.reset) {
req.session2.reset();
}
if (req.session && req.session.regenerate) {
return req.session.regenerate(() => {
return next();
});
}
},
isLoginUser: (req, res, next) => {
// 微信里边已经登录的时候,不再跳转登录
if (req.user.uid) {
AuthHelper.profile(req.user.uid).then(function(result) {
if (result.code !== 200) {
return next();
}
let refer = req.query.refer || decodeURI(req.cookies.refer) || config.siteUrl;
if (/sign|login/.test(refer)) {
refer = `${config.siteUrl}/home`;
}
refer = utils.yoho.refererLimit(refer);
return res.redirect(refer);
}).catch(() => {
return next();
});
} else {
return next();
}
},
check: (req, res) => {
let refer = req.query.refer;
// 短信推广的链接强制检查登录
if (req.user.uid) {
AuthHelper.profile(req.user.uid).then(function(result) {
if (result && result.code === 200) {
return res.redirect(refer);
}
return res.redirect(helpers.urlFormat('/signin.html', {
refer: refer
}));
}).catch(() => {
return res.redirect(helpers.urlFormat('/signin.html', {
refer: refer
}));
});
} else {
return res.redirect(helpers.urlFormat('/signin.html', {
refer: refer
}));
}
}
};
const wechat = {
login: (req, res, next) => {
req.session.originalUrl = 'true';
req.session.authState = uuid.v4();
req.session.cbUrl = req.query.cbUrl;
return passport.authenticate('weixin', {
state: req.session.authState
})(req, res, next);
},
callback: (req, res) => {
const openId = req.query.openId;
const nickname = req.query.nickname;
const headIcon = req.query.headImg;
doPassportCallback(openId, nickname, headIcon, 'wechat', req, res);
}
};
const sina = {
login: (req, res, next) => {
req.session.originalUrl = 'true';
req.session.authState = uuid.v4();
req.session.cbUrl = req.query.cbUrl;
return passport.authenticate('sina', {
state: req.session.authState
})(req, res, next);
},
callback: (req, res) => {
const openId = req.query.openId;
const nickname = req.query.nickname;
const headIcon = req.query.headImg;
doPassportCallback(openId, nickname, headIcon, 'sina', req, res);
}
};
const qq = {
login: (req, res, next) => {
req.session.originalUrl = 'true';
req.session.authState = uuid.v4();
req.session.cbUrl = req.query.cbUrl;
return passport.authenticate('qq', {
state: req.session.authState
})(req, res, next);
},
callback: (req, res, next) => {
if (req.session && req.session.authState && req.session.authState === req.query.state) {
passport.authenticate('qq', (err, user) => {
if (err) {
log.error(`qq authenticate error : ${JSON.stringify(err)}`);
return res.json({
code: 500,
message: 'authenticate error'
});
}
let nickname = user.nickname;
let openId = user.id;
let headIcon = user._json.figureurl_qq_2 || user._json.figureurl_2;
doPassportCallback(openId, nickname, headIcon, 'qq', req, res);
})(req, res, next);
} else {
log.error('Auth State Mismatch:' + req.originalUrl);
return res.redirect(loginPage);
}
}
};
exports.common = common;
exports.wechat = wechat;
exports.sina = sina;
exports.qq = qq;