@@ -3,6 +3,7 @@ var util = require('util');
3
3
4
4
var _ = require ( 'underscore' ) ;
5
5
var request = require ( 'request' ) ;
6
+ var prompt = require ( 'prompt' ) ;
6
7
7
8
var config = require ( '../config' ) ;
8
9
var h = require ( '../helper' ) ;
@@ -612,30 +613,39 @@ plugin.githubLogin = function(user, cb) {
612
613
if ( resp . request . uri . href !== urls . github_tf_redirect ) {
613
614
return requestLeetcodeAndSave ( _request , leetcodeUrl , user , cb ) ;
614
615
}
615
- // read two-factor code must be sync.
616
- const twoFactorcode = require ( 'prompt-sync' ) ( ) ( 'Please enter your two-factor code: ' ) ;
617
- const authenticityTokenTwoFactor = body . match ( / n a m e = " a u t h e n t i c i t y _ t o k e n " v a l u e = " ( .* ?) " / ) ;
618
- if ( authenticityTokenTwoFactor === null ) {
619
- return cb ( 'Get GitHub two-factor token failed' ) ;
620
- }
621
- const optionsTwoFactor = {
622
- url : urls . github_tf_session_request ,
623
- method : 'POST' ,
624
- headers : {
625
- 'Content-Type' : 'application/x-www-form-urlencoded' ,
626
- } ,
627
- followAllRedirects : true ,
628
- form : {
629
- 'otp' : twoFactorcode ,
630
- 'authenticity_token' : authenticityTokenTwoFactor [ 1 ] ,
631
- 'utf8' : encodeURIComponent ( '✓' ) ,
632
- } ,
633
- } ;
634
- _request ( optionsTwoFactor , function ( e , resp , body ) {
635
- if ( resp . request . uri . href === urls . github_tf_session_request ) {
636
- return cb ( 'Invalid two-factor code please check' ) ;
616
+ prompt . colors = false ;
617
+ prompt . message = '' ;
618
+ prompt . start ( ) ;
619
+ prompt . get ( [
620
+ {
621
+ name : 'twoFactorCode' ,
622
+ required : true
623
+ }
624
+ ] , function ( e , result ) {
625
+ if ( e ) return log . fail ( e ) ;
626
+ const authenticityTokenTwoFactor = body . match ( / n a m e = " a u t h e n t i c i t y _ t o k e n " v a l u e = " ( .* ?) " / ) ;
627
+ if ( authenticityTokenTwoFactor === null ) {
628
+ return cb ( 'Get GitHub two-factor token failed' ) ;
637
629
}
638
- requestLeetcodeAndSave ( _request , leetcodeUrl , user , cb ) ;
630
+ const optionsTwoFactor = {
631
+ url : urls . github_tf_session_request ,
632
+ method : 'POST' ,
633
+ headers : {
634
+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
635
+ } ,
636
+ followAllRedirects : true ,
637
+ form : {
638
+ 'otp' : result . twoFactorCode ,
639
+ 'authenticity_token' : authenticityTokenTwoFactor [ 1 ] ,
640
+ 'utf8' : encodeURIComponent ( '✓' ) ,
641
+ } ,
642
+ } ;
643
+ _request ( optionsTwoFactor , function ( e , resp , body ) {
644
+ if ( resp . request . uri . href === urls . github_tf_session_request ) {
645
+ return cb ( 'Invalid two-factor code please check' ) ;
646
+ }
647
+ requestLeetcodeAndSave ( _request , leetcodeUrl , user , cb ) ;
648
+ } ) ;
639
649
} ) ;
640
650
} ) ;
641
651
} ) ;
0 commit comments