3party-bind.js
7.84 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* Created by yoho on 2016/11/23.
*/
'use strict';
const _ = require('lodash');
const passport = require('passport');
const uuid = require('uuid');
const log = global.yoho.logger;
const userService = require('../models/user-service');
function bindSuccess(res, type) {
return res.render('home/user/bind-result', {
module: 'home',
page: 'bind-result',
layout: false,
type: type,
bindResult: 'success',
message: '绑定成功!'
});
}
function bindFail(res, message, type) {
return res.render('home/user/bind-result', {
module: 'home',
page: 'bind-result',
layout: false,
type: type,
bindResult: 'fail',
bindResultMessage: message || '绑定失败,请稍后再试!'
});
}
const bind3party = (req, res, user) => {
userService.bind3partyAccount(req.user.uid, user)
.then(ret => {
if (ret && ret.code === 200) {
return bindSuccess(res, user.sourceType);
}
return bindFail(res, ret && ret.message, user.sourceType);
})
.catch(err => {
return bindFail(res, err, user.sourceType);
});
};
const wechat = {
login: (req, res, next) => {
req.session.authState = uuid.v4();
return passport.authenticate('home-wechat', {
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('home-wechat', (err, user) => {
if (err) {
log.error(`wechat authenticate error : ${JSON.stringify(err)}`);
return bindFail(res, '微信登录失败,请稍后再试!', 'wechat');
} else {
bind3party(req, res, {
openId: user._json.unionid, // user._json.openid,
unionId: user._json.unionid || user.id,
nickname: user._json.nickname || user.displayName,
sourceType: 'wechat',
rawUser: user
});
}
})(req, res, next);
} else {
// return next(new Error('Wechat Auth State Mismatch'));
return bindFail(res, '微信登录失败,请稍后再试!', 'wechat');
}
}
};
const sina = {
login: (req, res, next) => {
req.session.authState = uuid.v4();
return passport.authenticate('home-sina', {
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('home-sina', (err, user) => {
if (err) {
log.error(`sina authenticate error : ${JSON.stringify(err)}`);
return bindFail(res, '新浪微博登录失败,请稍后再试!', 'sina');
}
let nickname = _.trim(user.screen_name);
let openId = user.id;
bind3party(req, res, {
openId: openId,
nickname: nickname,
sourceType: 'sina'
});
})(req, res, next);
} else {
// return next(new Error('Sina Auth State Mismatch'));
return bindFail(res, '微信登录失败,请稍后再试!', 'sina');
}
}
};
const qq = {
login: (req, res, next) => {
let type = req.query.type || 'yohobuy';
let authState = req.session.authState = (req.query.state || uuid.v4()) + '::' + type;
return passport.authenticate('home-qq', {
state: authState
})(req, res, next);
},
callback: (req, res, next) => {
if (req.session && req.session.authState && req.session.authState === req.query.state) {
passport.authenticate('home-qq', (err, user) => {
if (err) {
log.error(`qq authenticate error : ${JSON.stringify(err)}`);
return bindFail(res, 'QQ登录失败,请稍后再试!', 'qq');
}
let nickname = _.trim(user.nickname);
let openId = user.id;
bind3party(req, res, {
openId: openId,
nickname: nickname,
sourceType: 'qq'
});
})(req, res, next);
} else {
return bindFail(res, '微信登录失败,请稍后再试!', 'qq');
}
}
};
const alipay = {
login: (req, res, next) => {
return passport.authenticate('home-alipay')(req, res, next);
},
callback: (req, res, next) => {
passport.authenticate('home-alipay', (err, user) => {
if (err) {
log.error(`alipay authenticate error : ${JSON.stringify(err)}`);
return bindFail(res, '支付宝登录失败,请稍后再试!', 'alipay');
}
let nickname = _.trim(user.realName);
let openId = user.userId;
bind3party(req, res, {
openId: openId,
nickname: nickname,
sourceType: 'alipay'
});
})(req, res, next);
}
};
const douban = {
login: (req, res, next) => {
req.session.authState = uuid.v4() + '::bind';
return passport.authenticate('douban', {
state: req.session.authState
})(req, res, next);
},
callback: (req, res, next) => {
passport.authenticate('douban', (err, user) => {
if (err) {
log.error(`douban authenticate error : ${JSON.stringify(err)}`);
return bindFail(res, '豆瓣登录失败,请稍后再试!', 'douban');
}
let nickname = _.trim(user.displayName);
let openId = user.id;
bind3party(req, res, {
openId: openId,
nickname: nickname,
sourceType: 'douban'
});
})(req, res, next);
}
};
const renren = {
login: (req, res, next) => {
req.session.authState = uuid.v4();
return passport.authenticate('home-renren', {
state: req.session.authState
})(req, res, next);
},
callback: (req, res, next) => {
passport.authenticate('home-renren', (err, user) => {
if (err) {
log.error(`renren authenticate error : ${JSON.stringify(err)}`);
return bindFail(res, '人人网登录失败,请稍后再试!', 'renren');
}
let nickname = _.trim(user.displayName);
let openId = user.id;
bind3party(req, res, {
openId: openId,
nickname: nickname,
sourceType: 'renren'
});
})(req, res, next);
}
};
const cancelBind = (req, res) => {
let type = req.params.type;
if (!(req.get('Referer') || '').match(/yohobuy.com/)) {
return res.send({
code: 501,
message: '非法请求'
});
}
if (!type) {
res.status(400).json({
code: 400,
message: '解绑类型不正确!'
});
}
userService.cancelBind3partyAccount(req.user.uid, type)
.then(ret => {
if (ret && ret.code === 200) {
return res.send(ret);
}
return res.send({
code: 401
});
})
.catch(err => {
res.send({
code: 401,
message: err
});
});
};
module.exports = {
wechat: wechat,
sina: sina,
qq: qq,
alipay: alipay,
douban: douban,
renren: renren,
cancelBind,
bindFail,
bind3party
};