@@ -6,14 +6,14 @@ import { EventEmitter } from "events";
6
6
import * as vscode from "vscode" ;
7
7
import { leetCodeChannel } from "./leetCodeChannel" ;
8
8
import { leetCodeExecutor } from "./leetCodeExecutor" ;
9
- import { Endpoint , loginArgsMapping , urls , urlsCn , UserStatus } from "./shared" ;
9
+ import { Endpoint , IQuickItemEx , loginArgsMapping , urls , urlsCn , UserStatus } from "./shared" ;
10
10
import { createEnvOption } from "./utils/cpUtils" ;
11
11
import { DialogType , openUrl , promptForOpenOutputChannel } from "./utils/uiUtils" ;
12
12
import * as wsl from "./utils/wslUtils" ;
13
13
import { getLeetCodeEndpoint } from "./commands/plugin" ;
14
14
import { globalState } from "./globalState" ;
15
15
import { queryUserData } from "./request/query-user-data" ;
16
- import { parseQuery , sleep } from "./utils/toolUtils" ;
16
+ import { parseQuery } from "./utils/toolUtils" ;
17
17
18
18
class LeetCodeManager extends EventEmitter {
19
19
private currentUser : string | undefined ;
@@ -42,6 +42,19 @@ class LeetCodeManager extends EventEmitter {
42
42
}
43
43
}
44
44
45
+ private async updateUserStatusWithCookie ( cookie : string ) : Promise < void > {
46
+ globalState . setCookie ( cookie ) ;
47
+ const data = await queryUserData ( ) ;
48
+ globalState . setUserStatus ( data ) ;
49
+ await this . setCookieToCli ( cookie , data . username ) ;
50
+ if ( data . username ) {
51
+ vscode . window . showInformationMessage ( `Successfully ${ data . username } .` ) ;
52
+ this . currentUser = data . username ;
53
+ this . userStatus = UserStatus . SignedIn ;
54
+ this . emit ( "statusChanged" ) ;
55
+ }
56
+ }
57
+
45
58
public async handleUriSignIn ( uri : vscode . Uri ) : Promise < void > {
46
59
try {
47
60
await vscode . window . withProgress ( { location : vscode . ProgressLocation . Notification } , async ( progress : vscode . Progress < { } > ) => {
@@ -52,24 +65,56 @@ class LeetCodeManager extends EventEmitter {
52
65
promptForOpenOutputChannel ( `Failed to get cookie. Please log in again` , DialogType . error ) ;
53
66
return ;
54
67
}
55
- globalState . setCookie ( cookie ) ;
56
- const data = await queryUserData ( ) ;
57
- globalState . setUserStatus ( data ) ;
58
- await this . setCookieToCli ( cookie , data . username ) ;
59
- if ( data . username ) {
60
- vscode . window . showInformationMessage ( `Successfully ${ data . username } .` ) ;
61
- this . currentUser = data . username ;
62
- this . userStatus = UserStatus . SignedIn ;
63
- this . emit ( "statusChanged" ) ;
64
- }
68
+
69
+ this . updateUserStatusWithCookie ( cookie )
70
+
65
71
} ) ;
66
72
} catch ( error ) {
67
73
promptForOpenOutputChannel ( `Failed to log in. Please open the output channel for details` , DialogType . error ) ;
68
74
}
69
75
}
70
76
77
+ public async handleInputCookieSignIn ( ) : Promise < void > {
78
+ const cookie : string | undefined = await vscode . window . showInputBox ( {
79
+ prompt : 'Enter LeetCode Cookie' ,
80
+ placeHolder : 'You can find it in the Network requests within the Developer Tools.' ,
81
+ password : true ,
82
+ ignoreFocusOut : true ,
83
+ validateInput : ( s : string ) : string | undefined =>
84
+ s ? undefined : 'Cookie must not be empty' ,
85
+ } )
86
+
87
+ this . updateUserStatusWithCookie ( cookie || '' )
88
+ }
89
+
71
90
public async signIn ( ) : Promise < void > {
72
- openUrl ( this . getAuthLoginUrl ( ) ) ;
91
+ const picks : Array < IQuickItemEx < string > > = [ ]
92
+ picks . push (
93
+ {
94
+ label : 'Web Authorization' ,
95
+ detail : 'Open browser to authorize login on the website' ,
96
+ value : 'WebAuth' ,
97
+ description : '[Recommended]'
98
+ } ,
99
+ {
100
+ label : 'LeetCode Cookie' ,
101
+ detail : 'Use LeetCode cookie copied from browser to login' ,
102
+ value : 'Cookie' ,
103
+ }
104
+ )
105
+
106
+ const choice : IQuickItemEx < string > | undefined = await vscode . window . showQuickPick ( picks )
107
+ if ( ! choice ) {
108
+ return
109
+ }
110
+ const loginMethod : string = choice . value
111
+
112
+ if ( loginMethod === 'WebAuth' ) {
113
+ openUrl ( this . getAuthLoginUrl ( ) )
114
+ return
115
+ }
116
+
117
+ await this . handleInputCookieSignIn ( )
73
118
}
74
119
75
120
public async signOut ( ) : Promise < void > {
@@ -136,15 +181,16 @@ class LeetCodeManager extends EventEmitter {
136
181
} else if ( data . match ( this . failRegex ) ) {
137
182
childProc . stdin ?. end ( ) ;
138
183
return reject ( new Error ( "Faile to login" ) ) ;
184
+ } else if ( data . match ( / l o g i n : / ) ) {
185
+ childProc . stdin ?. write ( `${ name } \n` ) ;
186
+ } else if ( data . match ( / c o o k i e : / ) ) {
187
+ childProc . stdin ?. write ( `${ cookie } \n` ) ;
139
188
}
140
189
} ) ;
141
190
142
191
childProc . stderr ?. on ( "data" , ( data : string | Buffer ) => leetCodeChannel . append ( data . toString ( ) ) ) ;
143
192
144
193
childProc . on ( "error" , reject ) ;
145
- childProc . stdin ?. write ( `${ name } \n` ) ;
146
- await sleep ( 800 ) ;
147
- childProc . stdin ?. write ( `${ cookie } \n` ) ;
148
194
} ) ;
149
195
}
150
196
}
0 commit comments