Authored by htoooth

blk qq login

... ... @@ -6,6 +6,9 @@
'use strict';
const _ = require('lodash');
const Fp = require('lodash/fp');
const qs = require('querystring');
const passport = require('passport');
const uuid = require('uuid');
const md5 = require('md5');
... ... @@ -29,7 +32,7 @@ function doPassportCallback(req, res, user) {
if (/sign|login/.test(refer)) {
refer = config.siteUrl;
}
if (user.openId && user.nickname) {
if (user.openId) {
let signinByOpenID;
if (user.sourceType === 'wechat') {
... ... @@ -80,7 +83,7 @@ const common = {
},
needCaptcha: (req, res, next) => {
let account = req.query.account;
let result = { code: 400, message: '', data: '' };
let result = {code: 400, message: '', data: ''};
if (account) {
let errorLoginKey = 'account_errorlogin_' + account;
... ... @@ -88,7 +91,7 @@ const common = {
cache.get(errorLoginKey).then(errloginTimes => {
errloginTimes = parseInt(errloginTimes, 0) || 0;
if (!isNaN(errloginTimes) && errloginTimes >= 3) {
result.data = { needCaptcha: true };
result.data = {needCaptcha: true};
}
res.json(result);
}).catch(next);
... ... @@ -278,12 +281,18 @@ const sina = {
const qq = {
login: (req, res, next) => {
req.session = req.session || {};
req.session.authState = uuid.v4();
let type = req.query.type || 'yohobuy';
let authState = req.session.authState = (req.query.state || uuid.v4()) + '::' + type;
return passport.authenticate('qq', {
state: req.session.authState
state: authState
})(req, res, next);
},
callback: (req, res, next) => {
let auth = Fp.compose(Fp.head, Fp.split('::'))(req.query.state);
let type = Fp.compose(Fp.nth(1), Fp.split('::'))(req.query.state);
if (req.session && req.session.authState && req.session.authState === req.query.state) {
passport.authenticate('qq', (err, user) => {
if (err) {
... ... @@ -293,11 +302,16 @@ const qq = {
let nickname = _.trim(user.nickname);
let openId = user.id;
doPassportCallback(req, res, {
openId: openId,
nickname: nickname,
sourceType: 'qq'
}).catch(next);
if (type === 'yohoblk') {
return res.redirect('//www.yohoblk.com/passport/login/qq/callback?' +
qs.stringify({openid: openId, nickname: nickname, state: auth}));
} else {
doPassportCallback(req, res, {
openId: openId,
nickname: nickname,
sourceType: 'qq'
}).catch(next);
}
})(req, res, next);
} else {
return next(new Error('Auth State Mismatch'));
... ...