...
|
...
|
@@ -21,7 +21,7 @@ const loginPage = `${config.siteUrl}/passport/login/index`; |
|
|
const SIGNIN_LEFT_BANNER_CODE = 'db350894e01e90eac55cd3a13ad77331';
|
|
|
|
|
|
// 第三方登录回调
|
|
|
function doPassportCallback(req, res, next, user) {
|
|
|
function doPassportCallback(req, res, user) {
|
|
|
let shoppingKey = cookie.getShoppingKey(req);
|
|
|
let refer = req.cookies.refer;
|
|
|
|
...
|
...
|
@@ -60,7 +60,7 @@ function doPassportCallback(req, res, next, user) { |
|
|
} else if (result.code === 200 && result.data.uid) {
|
|
|
return AuthHelper.syncUserSession(result.data.uid, req, res).then(() => {
|
|
|
return refer;
|
|
|
}).catch(next);
|
|
|
});
|
|
|
}
|
|
|
}).then((redirectTo) => {
|
|
|
res.redirect(redirectTo);
|
...
|
...
|
@@ -83,26 +83,6 @@ const common = { |
|
|
domain: 'yohobuy.com'
|
|
|
});
|
|
|
next();
|
|
|
},
|
|
|
ipFilter: (req, res, next) => {
|
|
|
let account = req.body.account;
|
|
|
let ip = req.ip;
|
|
|
|
|
|
// let errorLoginKey = 'account_errorlogin_' + account;
|
|
|
let accountKey = 'account_signin_' + account;
|
|
|
let ipKey = 'ip_signin_' + ip;
|
|
|
|
|
|
// let errLoginTimes = cache.get(errorLoginKey) || 0;
|
|
|
let accountTimes = cache.get(accountKey) || 0;
|
|
|
let ipTimes = cache.get(ipKey) || 0;
|
|
|
|
|
|
if (accountTimes >= 10) {
|
|
|
res.json({ code: 400, message: '您的账号已被暂时锁定,请稍后再试', data: '' });
|
|
|
} else if (ipTimes >= 100) {
|
|
|
res.json({ code: 400, message: '您尝试的次数过多,账号已被暂时锁定,请稍后再试', data: '' });
|
|
|
} else {
|
|
|
return next();
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
...
|
...
|
@@ -135,6 +115,7 @@ const local = { |
|
|
forgetPwd: helpers.urlFormat('/passport/back/index'),
|
|
|
fastReg: helpers.urlFormat('/reg.html'),
|
|
|
weixinLogin: helpers.urlFormat('/passport/autosign/wechat'),
|
|
|
qqLogin: helpers.urlFormat('/passport/autosign/qq'),
|
|
|
weiboLogin: helpers.urlFormat('/passport/autosign/sina'),
|
|
|
alipayLogin: helpers.urlFormat('/passport/autosign/alipay'),
|
|
|
doubanLogin: helpers.urlFormat('/passport/autosign/douban'),
|
...
|
...
|
@@ -153,11 +134,20 @@ const local = { |
|
|
res.json({
|
|
|
code: 400,
|
|
|
message: err,
|
|
|
data: ''
|
|
|
data: user || {}
|
|
|
});
|
|
|
} else {
|
|
|
let isRemember = req.body.isRemember;
|
|
|
let refer = req.cookies.refer;
|
|
|
|
|
|
if (isRemember) {
|
|
|
AuthHelper.rememberAccount({
|
|
|
area: req.body.areaCode || '86',
|
|
|
account: req.body.account,
|
|
|
password: req.body.password
|
|
|
}, req, res);
|
|
|
}
|
|
|
|
|
|
if (refer) {
|
|
|
refer = decodeURI(req.cookies.refer);
|
|
|
} else {
|
...
|
...
|
@@ -177,33 +167,142 @@ const local = { |
|
|
});
|
|
|
}
|
|
|
})(req, res, next);
|
|
|
},
|
|
|
logout: (req, res) => {
|
|
|
req.session = null;
|
|
|
|
|
|
res.clearCookie('_UID', {
|
|
|
domain: 'yohobuy.com'
|
|
|
});
|
|
|
res.clearCookie('_TOKEN', {
|
|
|
domain: 'yohobuy.com'
|
|
|
});
|
|
|
res.clearCookie('_SPK');
|
|
|
res.clearCookie('_g');
|
|
|
res.clearCookie('isRemember');
|
|
|
res.clearCookie('remem');
|
|
|
let refer = req.get('Referer') || config.siteUrl;
|
|
|
|
|
|
res.redirect(refer);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
const wechat = {
|
|
|
login: (req, res, next) => {
|
|
|
req.session = req.session || {};
|
|
|
req.session.authState = uuid.v4();
|
|
|
return passport.authenticate('wechat', {
|
|
|
state: uuid.v4()
|
|
|
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('wechat', (err, user) => {
|
|
|
if (err) {
|
|
|
log.error(`wechat authenticate error : ${JSON.stringify(err)}`);
|
|
|
return res.redirect(loginPage);
|
|
|
}
|
|
|
|
|
|
doPassportCallback(req, res, {
|
|
|
openId: user._json.openid,
|
|
|
unionId: user._json.unionid || user.id,
|
|
|
nickname: user._json.nickname || user.displayName,
|
|
|
sourceType: 'wechat',
|
|
|
rawUser: user
|
|
|
}).catch(next);
|
|
|
})(req, res, next);
|
|
|
} else {
|
|
|
return next(new Error('Auth State Mismatch'));
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
const sina = {
|
|
|
login: (req, res, next) => {
|
|
|
req.session = req.session || {};
|
|
|
req.session.authState = uuid.v4();
|
|
|
return passport.authenticate('sina', {
|
|
|
state: req.session.authState
|
|
|
})(req, res, next);
|
|
|
},
|
|
|
callback: (req, res, next) => {
|
|
|
passport.authenticate('wechat', (err, user) => {
|
|
|
if (req.session && req.session.authState && req.session.authState === req.query.state) {
|
|
|
passport.authenticate('sina', (err, user) => {
|
|
|
if (err) {
|
|
|
log.error(`sina authenticate error : ${JSON.stringify(err)}`);
|
|
|
return res.redirect(loginPage);
|
|
|
}
|
|
|
let nickname = user.screen_name;
|
|
|
let openId = user.id;
|
|
|
|
|
|
doPassportCallback(req, res, {
|
|
|
openId: openId,
|
|
|
nickname: nickname,
|
|
|
sourceType: 'sina'
|
|
|
}).catch(next);
|
|
|
})(req, res, next);
|
|
|
} else {
|
|
|
return next(new Error('Auth State Mismatch'));
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
const qq = {
|
|
|
login: (req, res, next) => {
|
|
|
req.session = req.session || {};
|
|
|
req.session.authState = uuid.v4();
|
|
|
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.redirect(loginPage);
|
|
|
}
|
|
|
let nickname = user.nickname;
|
|
|
let openId = user.openid;
|
|
|
|
|
|
doPassportCallback(req, res, {
|
|
|
openId: openId,
|
|
|
nickname: nickname,
|
|
|
sourceType: 'qq'
|
|
|
}).catch(next);
|
|
|
})(req, res, next);
|
|
|
} else {
|
|
|
return next(new Error('Auth State Mismatch'));
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
const alipay = {
|
|
|
login: (req, res, next) => {
|
|
|
return passport.authenticate('alipay')(req, res, next);
|
|
|
},
|
|
|
callback: (req, res, next) => {
|
|
|
passport.authenticate('alipay', (err, user) => {
|
|
|
if (err) {
|
|
|
log.error(`wechat authenticate error : ${JSON.stringify(err)}`);
|
|
|
log.error(`alipay authenticate error : ${JSON.stringify(err)}`);
|
|
|
return res.redirect(loginPage);
|
|
|
}
|
|
|
let nickname = user.realName;
|
|
|
let openId = user.userId;
|
|
|
|
|
|
doPassportCallback(req, res, next, {
|
|
|
openId: user._json.openid,
|
|
|
unionId: user._json.unionid || user.id,
|
|
|
nickname: user._json.nickname || user.displayName,
|
|
|
sourceType: 'wechat',
|
|
|
rawUser: user
|
|
|
});
|
|
|
doPassportCallback(req, res, {
|
|
|
openId: openId,
|
|
|
nickname: nickname,
|
|
|
sourceType: 'alipay'
|
|
|
}).catch(next);
|
|
|
})(req, res, next);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
exports.common = common;
|
|
|
exports.wechat = wechat;
|
|
|
exports.local = local;
|
|
|
exports.sina = sina;
|
|
|
exports.qq = qq;
|
|
|
exports.alipay = alipay; |
...
|
...
|
|