Authored by htoooth

blk qq login

@@ -6,6 +6,9 @@ @@ -6,6 +6,9 @@
6 'use strict'; 6 'use strict';
7 7
8 const _ = require('lodash'); 8 const _ = require('lodash');
  9 +const Fp = require('lodash/fp');
  10 +const qs = require('querystring');
  11 +
9 const passport = require('passport'); 12 const passport = require('passport');
10 const uuid = require('uuid'); 13 const uuid = require('uuid');
11 const md5 = require('md5'); 14 const md5 = require('md5');
@@ -29,7 +32,7 @@ function doPassportCallback(req, res, user) { @@ -29,7 +32,7 @@ function doPassportCallback(req, res, user) {
29 if (/sign|login/.test(refer)) { 32 if (/sign|login/.test(refer)) {
30 refer = config.siteUrl; 33 refer = config.siteUrl;
31 } 34 }
32 - if (user.openId && user.nickname) { 35 + if (user.openId) {
33 let signinByOpenID; 36 let signinByOpenID;
34 37
35 if (user.sourceType === 'wechat') { 38 if (user.sourceType === 'wechat') {
@@ -80,7 +83,7 @@ const common = { @@ -80,7 +83,7 @@ const common = {
80 }, 83 },
81 needCaptcha: (req, res, next) => { 84 needCaptcha: (req, res, next) => {
82 let account = req.query.account; 85 let account = req.query.account;
83 - let result = { code: 400, message: '', data: '' }; 86 + let result = {code: 400, message: '', data: ''};
84 87
85 if (account) { 88 if (account) {
86 let errorLoginKey = 'account_errorlogin_' + account; 89 let errorLoginKey = 'account_errorlogin_' + account;
@@ -88,7 +91,7 @@ const common = { @@ -88,7 +91,7 @@ const common = {
88 cache.get(errorLoginKey).then(errloginTimes => { 91 cache.get(errorLoginKey).then(errloginTimes => {
89 errloginTimes = parseInt(errloginTimes, 0) || 0; 92 errloginTimes = parseInt(errloginTimes, 0) || 0;
90 if (!isNaN(errloginTimes) && errloginTimes >= 3) { 93 if (!isNaN(errloginTimes) && errloginTimes >= 3) {
91 - result.data = { needCaptcha: true }; 94 + result.data = {needCaptcha: true};
92 } 95 }
93 res.json(result); 96 res.json(result);
94 }).catch(next); 97 }).catch(next);
@@ -278,12 +281,18 @@ const sina = { @@ -278,12 +281,18 @@ const sina = {
278 const qq = { 281 const qq = {
279 login: (req, res, next) => { 282 login: (req, res, next) => {
280 req.session = req.session || {}; 283 req.session = req.session || {};
281 - req.session.authState = uuid.v4(); 284 +
  285 + let type = req.query.type || 'yohobuy';
  286 + let authState = req.session.authState = (req.query.state || uuid.v4()) + '::' + type;
  287 +
282 return passport.authenticate('qq', { 288 return passport.authenticate('qq', {
283 - state: req.session.authState 289 + state: authState
284 })(req, res, next); 290 })(req, res, next);
285 }, 291 },
286 callback: (req, res, next) => { 292 callback: (req, res, next) => {
  293 + let auth = Fp.compose(Fp.head, Fp.split('::'))(req.query.state);
  294 + let type = Fp.compose(Fp.nth(1), Fp.split('::'))(req.query.state);
  295 +
287 if (req.session && req.session.authState && req.session.authState === req.query.state) { 296 if (req.session && req.session.authState && req.session.authState === req.query.state) {
288 passport.authenticate('qq', (err, user) => { 297 passport.authenticate('qq', (err, user) => {
289 if (err) { 298 if (err) {
@@ -293,11 +302,16 @@ const qq = { @@ -293,11 +302,16 @@ const qq = {
293 let nickname = _.trim(user.nickname); 302 let nickname = _.trim(user.nickname);
294 let openId = user.id; 303 let openId = user.id;
295 304
296 - doPassportCallback(req, res, {  
297 - openId: openId,  
298 - nickname: nickname,  
299 - sourceType: 'qq'  
300 - }).catch(next); 305 + if (type === 'yohoblk') {
  306 + return res.redirect('//www.yohoblk.com/passport/login/qq/callback?' +
  307 + qs.stringify({openid: openId, nickname: nickname, state: auth}));
  308 + } else {
  309 + doPassportCallback(req, res, {
  310 + openId: openId,
  311 + nickname: nickname,
  312 + sourceType: 'qq'
  313 + }).catch(next);
  314 + }
301 })(req, res, next); 315 })(req, res, next);
302 } else { 316 } else {
303 return next(new Error('Auth State Mismatch')); 317 return next(new Error('Auth State Mismatch'));