@@ -471,8 +471,8 @@ plugin.deleteSession = function(session, cb) {
471
471
} ;
472
472
473
473
plugin . signin = function ( user , cb ) {
474
- log . debug ( 'running leetcode.signin' ) ;
475
- const spin = h . spin ( 'Signing in leetcode.com' ) ;
474
+ const isCN = config . app === ' leetcode.cn' ;
475
+ const spin = isCN ? h . spin ( 'Signing in leetcode-cn.com' ) : h . spin ( 'Signing in leetcode.com' ) ;
476
476
request ( config . sys . urls . login , function ( e , resp , body ) {
477
477
spin . stop ( ) ;
478
478
e = plugin . checkError ( e , resp , 200 ) ;
@@ -538,26 +538,18 @@ plugin.login = function(user, cb) {
538
538
} ) ;
539
539
} ;
540
540
541
- function parseCookie ( cookie , cb ) {
541
+ function parseCookie ( cookie , body , cb ) {
542
+ const isCN = config . app === 'leetcode.cn' ;
542
543
const SessionPattern = / L E E T C O D E _ S E S S I O N = ( .+ ?) ( ; | $ ) / ;
543
- const csrfPattern = / c s r f t o k e n = ( .+ ?) ( ; | $ ) / ;
544
- const reSessionResult = SessionPattern . exec ( cookie ) ;
545
- const reCsrfResult = csrfPattern . exec ( cookie ) ;
546
- if ( reSessionResult === null || reCsrfResult === null ) {
547
- return cb ( 'invalid cookie?' ) ;
544
+ let csrfPattern ;
545
+ // leetcode-cn.com Cookie is not the same as leetcode.com in third parties
546
+ if ( isCN ) {
547
+ csrfPattern = / n a m e = " c s r f m i d d l e w a r e t o k e n " v a l u e = " ( .* ?) " / ;
548
+ } else {
549
+ csrfPattern = / c s r f t o k e n = ( .+ ?) ( ; | $ ) / ;
548
550
}
549
- return {
550
- sessionId : reSessionResult [ 1 ] ,
551
- sessionCSRF : reCsrfResult [ 1 ] ,
552
- } ;
553
- }
554
-
555
- // leetcode-cn.com Cookie is not the same as leetcode.com in third parties
556
- function parseCNCookie ( cookie , body , cb ) {
557
- const SessionPattern = / L E E T C O D E _ S E S S I O N = ( .+ ?) ( ; | $ ) / ;
558
- const csrfPattern = / n a m e = " c s r f m i d d l e w a r e t o k e n " v a l u e = " ( .* ?) " / ;
559
551
const reSessionResult = SessionPattern . exec ( cookie ) ;
560
- const reCsrfResult = csrfPattern . exec ( body ) ;
552
+ const reCsrfResult = csrfPattern . exec ( isCN ? body : cookie ) ;
561
553
if ( reSessionResult === null || reCsrfResult === null ) {
562
554
return cb ( 'invalid cookie?' ) ;
563
555
}
@@ -567,16 +559,13 @@ function parseCNCookie(cookie, body, cb) {
567
559
} ;
568
560
}
569
561
570
- function requestLeetcodeAndSave ( request , leetcodeUrl , user , cb , party ) {
562
+ function requestLeetcodeAndSave ( request , leetcodeUrl , user , cb ) {
571
563
request . get ( { url : leetcodeUrl } , function ( e , resp , body ) {
572
564
const redirectUri = resp . request . uri . href ;
573
565
if ( redirectUri !== config . sys . urls . leetcode_redirect ) {
574
- return cb ( ` ${ party } login failed or ${ party } did not connect to LeetCode` ) ;
566
+ return cb ( 'Login failed. Please make sure the credential is correct.' ) ;
575
567
}
576
- let cookieData = { } ;
577
- if ( leetcodeUrl . includes ( 'cn' ) ) {
578
- cookieData = parseCNCookie ( resp . request . headers . cookie , body , cb ) ;
579
- } else cookieData = parseCookie ( resp . request . headers . cookie , cb ) ;
568
+ const cookieData = parseCookie ( resp . request . headers . cookie , body , cb ) ;
580
569
user . sessionId = cookieData . sessionId ;
581
570
user . sessionCSRF = cookieData . sessionCSRF ;
582
571
session . saveUser ( user ) ;
@@ -621,7 +610,6 @@ plugin.githubLogin = function(user, cb) {
621
610
return cb ( 'GitHub login failed' ) ;
622
611
}
623
612
if ( resp . request . uri . href === urls . github_tf_redirect ) {
624
- cb ( 'Your GitHub are using two-factor authentication' ) ;
625
613
// read two-factor code must be sync.
626
614
const twoFactorcode = require ( 'prompt-sync' ) ( ) ( 'Please enter your two-factor code: ' ) ;
627
615
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 = " ( .* ?) " / ) ;
@@ -645,10 +633,10 @@ plugin.githubLogin = function(user, cb) {
645
633
if ( resp . request . uri . href === urls . github_tf_session_request ) {
646
634
return cb ( 'Invalid two-factor code please check' ) ;
647
635
}
648
- requestLeetcodeAndSave ( _request , leetcodeUrl , user , cb , config . sys . login_methods . GitHub ) ;
636
+ requestLeetcodeAndSave ( _request , leetcodeUrl , user , cb ) ;
649
637
} ) ;
650
638
} else {
651
- requestLeetcodeAndSave ( _request , leetcodeUrl , user , cb , config . sys . login_methods . GitHub ) ;
639
+ requestLeetcodeAndSave ( _request , leetcodeUrl , user , cb ) ;
652
640
}
653
641
} ) ;
654
642
} ) ;
@@ -689,7 +677,7 @@ plugin.linkedinLogin = function(user, cb) {
689
677
if ( resp . statusCode !== 200 ) {
690
678
return cb ( 'LinkedIn login failed' ) ;
691
679
}
692
- requestLeetcodeAndSave ( _request , leetcodeUrl , user , cb , config . sys . login_methods . LinkedIn ) ;
680
+ requestLeetcodeAndSave ( _request , leetcodeUrl , user , cb ) ;
693
681
} ) ;
694
682
} ) ;
695
683
} ;
0 commit comments