|
|
/**
|
|
|
* 登录
|
|
|
* @author: Bi Kai<kai.bi@yoho.cn>
|
|
|
* @date: 2016/05/09
|
|
|
*/
|
|
|
'use strict';
|
|
|
|
|
|
const library = '../../../library';
|
|
|
const passport = require('passport');
|
|
|
const uuid = require('uuid');
|
|
|
const cookie = require(`${library}/cookie`);
|
|
|
const helpers = require(`${library}/helpers`);
|
|
|
const log = require(`${library}/logger`);
|
|
|
const config = require('../../../config/common');
|
|
|
const AuthHelper = require('../models/auth-helper');
|
|
|
|
|
|
const loginPage = `${config.siteUrl}/passport/login/index`;
|
|
|
|
|
|
// 第三方登录回调
|
|
|
function doPassportCallback(req, res, next, user) {
|
|
|
let shoppingKey = cookie.getShoppingKey(req);
|
|
|
let refer = req.cookies.refer;
|
|
|
|
|
|
if (refer) {
|
|
|
refer = decodeURI(req.cookies.refer);
|
|
|
} else {
|
|
|
refer = config.siteUrl;
|
|
|
}
|
|
|
|
|
|
if (/sign|login/.test(refer)) {
|
|
|
refer = config.siteUrl;
|
|
|
}
|
|
|
if (user.openId && user.nickname) {
|
|
|
let signinByOpenID;
|
|
|
|
|
|
if (user.sourceType === 'wechat') {
|
|
|
|
|
|
// PC 的微信登录之前使用了 open_id, 所以需要特别的接口处理
|
|
|
signinByOpenID = AuthHelper.signinByOpenID(user.nickname, user.openId, user.sourceType, shoppingKey);
|
|
|
} else {
|
|
|
signinByOpenID = AuthHelper.signinByOpenID(user.nickname, user.openId, user.sourceType, shoppingKey);
|
|
|
}
|
|
|
|
|
|
signinByOpenID.then((result) => {
|
|
|
if (result.data['is_bind'] && result.data['is_bind'] === 'N') { //eslint-disable-line
|
|
|
return helpers.urlFormat('/passport/bind/index', {
|
|
|
openId: user.openId,
|
|
|
sourceType: user.sourceType,
|
|
|
refer: refer
|
|
|
});
|
|
|
} else if (result.code === 200 && result.data.uid) {
|
|
|
return AuthHelper.syncUserSession(result.data.uid, req, res).then(() => {
|
|
|
return refer;
|
|
|
});
|
|
|
}
|
|
|
}).then((redirectTo) => {
|
|
|
return res.redirect(redirectTo);
|
|
|
}).catch(next);
|
|
|
} else {
|
|
|
res.redirect(loginPage);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
const wechat = {
|
|
|
beforeLogin: (req, res, next) => {
|
|
|
let refer = req.query.refer;
|
|
|
|
|
|
if (!refer) {
|
|
|
refer = req.get('Referer');
|
|
|
}
|
|
|
refer && res.cookie('refer', encodeURI(refer), {
|
|
|
domain: 'yohobuy.com'
|
|
|
});
|
|
|
next();
|
|
|
},
|
|
|
login: (req, res, next) => {
|
|
|
return passport.authenticate('wechat', {
|
|
|
state: uuid.v4()
|
|
|
})(req, res, next);
|
|
|
},
|
|
|
callback: (req, res, next) => {
|
|
|
passport.authenticate('wechat', (err, user) => {
|
|
|
if (err) {
|
|
|
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;
|
|
|
|
|
|
doPassportCallback(req, res, next, {
|
|
|
openId: openId,
|
|
|
nickname: nickname,
|
|
|
sourceType: 'wechat',
|
|
|
rawUser: user
|
|
|
});
|
|
|
})(req, res, next);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
exports.wechat = wechat; |
...
|
...
|
|