...
|
...
|
@@ -9,9 +9,19 @@ const utils = global.yoho.utils; |
|
|
const AuthHelper = require('../models/auth-helper');
|
|
|
const loginPage = `${config.siteUrl}/signin.html`;
|
|
|
|
|
|
function doPassportCallback(openId, nickname, sourceType, req, res) { // eslint-disable-line
|
|
|
if (openId && nickname) {
|
|
|
// TODO 授权成功、实现助力
|
|
|
function doPassportCallback(openId, nickname, source, req, res) { // eslint-disable-line
|
|
|
if (openId && nickname && source) {
|
|
|
req.session._THIRD_LOGIN_INFO = {
|
|
|
openId,
|
|
|
nickname,
|
|
|
source
|
|
|
};
|
|
|
|
|
|
// 第三方登录
|
|
|
req.session._THIRD_LOGIN = true;
|
|
|
req.session._THIRD_LOGIN_SOURCE = source;
|
|
|
|
|
|
res.status(301).redirect(`${req.session.cbUrl}`);
|
|
|
} else {
|
|
|
return Promise.reject('missing third party login openId or nickname');
|
|
|
}
|
...
|
...
|
@@ -133,73 +143,46 @@ const common = { |
|
|
|
|
|
const wechat = {
|
|
|
login: (req, res, next) => {
|
|
|
// 设置为原链接标识originalUrl
|
|
|
req.session.originalUrl = 'true';
|
|
|
req.session.authState = uuid.v4();
|
|
|
delete req.session._WX_PASS_LOGIN;
|
|
|
req.session.cbUrl = req.query.cbUrl;
|
|
|
|
|
|
return passport.authenticate('weixin', {
|
|
|
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('weixin', (err, user) => {
|
|
|
if (err || !user) {
|
|
|
log.error(`wechat authenticate error : ${JSON.stringify(err)}`);
|
|
|
return res.redirect(loginPage);
|
|
|
}
|
|
|
let nickname = user._json.nickname || user.displayName;
|
|
|
let openId = user._json.unionid || user.id;
|
|
|
callback: (req, res) => {
|
|
|
const openId = req.query.openId;
|
|
|
const nickname = req.query.nickname;
|
|
|
|
|
|
res.session._WX_OPENID = _.get(user, '_json.openid');
|
|
|
res.session._WX_UNIONID = _.get(user, '_json.unionid');
|
|
|
res.session._LOGIN_TYPE = 4;
|
|
|
doPassportCallback(openId, nickname, 'wechat', req, res).catch(next);
|
|
|
})(req, res, next);
|
|
|
} else {
|
|
|
log.error('Auth State Mismatch:' + req.originalUrl);
|
|
|
return res.redirect(loginPage);
|
|
|
}
|
|
|
doPassportCallback(openId, nickname, 'qq', req, res);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
const sina = {
|
|
|
login: (req, res, next) => {
|
|
|
// 设置为原链接标识originalUrl
|
|
|
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, next) => {
|
|
|
if (req.session && req.session.authState && req.session.authState === req.query.state) {
|
|
|
passport.authenticate('sina', (err, user) => {
|
|
|
if (err || !user) {
|
|
|
log.error(`sina authenticate error : ${JSON.stringify(err)}`);
|
|
|
return res.redirect(loginPage);
|
|
|
}
|
|
|
let nickname = user.screen_name;
|
|
|
let openId = user.id;
|
|
|
callback: (req, res) => {
|
|
|
const openId = req.query.openId;
|
|
|
const nickname = req.query.nickname;
|
|
|
|
|
|
res.cookie('_LOGIN_TYPE', 2, {
|
|
|
domain: 'm.yohobuy.com'
|
|
|
});
|
|
|
doPassportCallback(openId, nickname, 'sina', req, res).catch(next);
|
|
|
})(req, res, next);
|
|
|
} else {
|
|
|
log.error('Auth State Mismatch:' + req.originalUrl);
|
|
|
return res.redirect(loginPage);
|
|
|
}
|
|
|
doPassportCallback(openId, nickname, 'sina', req, res);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
const qq = {
|
|
|
login: (req, res, next) => {
|
|
|
// 设置为原链接标识originalUrl
|
|
|
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);
|
...
|
...
|
@@ -210,15 +193,16 @@ const qq = { |
|
|
if (err) {
|
|
|
log.error(`qq authenticate error : ${JSON.stringify(err)}`);
|
|
|
|
|
|
// TODO 授权失败
|
|
|
// return res.redirect(loginPage);
|
|
|
return res.json({
|
|
|
code: 500,
|
|
|
message: 'authenticate error'
|
|
|
});
|
|
|
}
|
|
|
|
|
|
let nickname = user.nickname;
|
|
|
let openId = user.id;
|
|
|
|
|
|
req.session._LOGIN_TYPE = 1; // 1: QQ授权登录
|
|
|
doPassportCallback(openId, nickname, 'qq', req, res).catch(next);
|
|
|
doPassportCallback(openId, nickname, 'qq', req, res);
|
|
|
})(req, res, next);
|
|
|
} else {
|
|
|
log.error('Auth State Mismatch:' + req.originalUrl);
|
...
|
...
|
@@ -227,31 +211,7 @@ const qq = { |
|
|
}
|
|
|
};
|
|
|
|
|
|
const alipay = {
|
|
|
login: (req, res, next) => {
|
|
|
// 设置为原链接标识originalUrl
|
|
|
req.session.originalUrl = 'true';
|
|
|
return passport.authenticate('alipay')(req, res, next);
|
|
|
},
|
|
|
callback: (req, res, next) => {
|
|
|
passport.authenticate('alipay', (err, user) => {
|
|
|
if (err || !user) {
|
|
|
log.error(`alipay authenticate error : ${JSON.stringify(err)}`);
|
|
|
return res.redirect(loginPage);
|
|
|
}
|
|
|
let nickname = user.realName;
|
|
|
let openId = user.userId;
|
|
|
|
|
|
res.cookie('_LOGIN_TYPE', 3, {
|
|
|
domain: 'm.yohobuy.com'
|
|
|
});
|
|
|
doPassportCallback(openId, nickname, 'alipay', req, res).catch(next);
|
|
|
})(req, res, next);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
exports.common = common;
|
|
|
exports.wechat = wechat;
|
|
|
exports.sina = sina;
|
|
|
exports.qq = qq; |
|
|
exports.alipay = alipay; |
...
|
...
|
|