Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ad857aa

Browse files
feat: re-add cookie-based login method
1 parent b495812 commit ad857aa

File tree

1 file changed

+67
-16
lines changed

1 file changed

+67
-16
lines changed

‎src/leetCodeManager.ts‎

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import { EventEmitter } from "events";
66
import * as vscode from "vscode";
77
import { leetCodeChannel } from "./leetCodeChannel";
88
import { leetCodeExecutor } from "./leetCodeExecutor";
9-
import { Endpoint, loginArgsMapping, urls, urlsCn, UserStatus } from "./shared";
9+
import { Endpoint, IQuickItemEx,loginArgsMapping, urls, urlsCn, UserStatus } from "./shared";
1010
import { createEnvOption } from "./utils/cpUtils";
1111
import { DialogType, openUrl, promptForOpenOutputChannel } from "./utils/uiUtils";
1212
import * as wsl from "./utils/wslUtils";
1313
import { getLeetCodeEndpoint } from "./commands/plugin";
1414
import { globalState } from "./globalState";
1515
import { queryUserData } from "./request/query-user-data";
16-
import { parseQuery,sleep } from "./utils/toolUtils";
16+
import { parseQuery } from "./utils/toolUtils";
1717

1818
class LeetCodeManager extends EventEmitter {
1919
private currentUser: string | undefined;
@@ -42,6 +42,19 @@ class LeetCodeManager extends EventEmitter {
4242
}
4343
}
4444

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+
4558
public async handleUriSignIn(uri: vscode.Uri): Promise<void> {
4659
try {
4760
await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification }, async (progress: vscode.Progress<{}>) => {
@@ -52,24 +65,61 @@ class LeetCodeManager extends EventEmitter {
5265
promptForOpenOutputChannel(`Failed to get cookie. Please log in again`, DialogType.error);
5366
return;
5467
}
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+
await this.updateUserStatusWithCookie(cookie)
70+
6571
});
6672
} catch (error) {
6773
promptForOpenOutputChannel(`Failed to log in. Please open the output channel for details`, DialogType.error);
6874
}
6975
}
7076

77+
public async handleInputCookieSignIn(): Promise<void> {
78+
const cookie: string | undefined = await vscode.window.showInputBox({
79+
prompt: 'Enter LeetCode Cookie',
80+
password: true,
81+
ignoreFocusOut: true,
82+
validateInput: (s: string): string | undefined =>
83+
s ? undefined : 'Cookie must not be empty',
84+
})
85+
86+
await this.updateUserStatusWithCookie(cookie || '')
87+
}
88+
7189
public async signIn(): Promise<void> {
72-
openUrl(this.getAuthLoginUrl());
90+
const picks: Array<IQuickItemEx<string>> = []
91+
picks.push(
92+
{
93+
label: 'Web Authorization',
94+
detail: 'Open browser to authorize login on the website',
95+
value: 'WebAuth',
96+
description: '[Recommended]'
97+
},
98+
{
99+
label: 'LeetCode Cookie',
100+
detail: 'Use LeetCode cookie copied from browser to login',
101+
value: 'Cookie',
102+
}
103+
)
104+
105+
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(picks)
106+
if (!choice) {
107+
return
108+
}
109+
const loginMethod: string = choice.value
110+
111+
if (loginMethod === 'WebAuth') {
112+
openUrl(this.getAuthLoginUrl())
113+
return
114+
}
115+
116+
try {
117+
await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, title: "Fetching user data..." }, async () => {
118+
await this.handleInputCookieSignIn()
119+
});
120+
} catch (error) {
121+
promptForOpenOutputChannel(`Failed to log in. Please open the output channel for details`, DialogType.error);
122+
}
73123
}
74124

75125
public async signOut(): Promise<void> {
@@ -136,15 +186,16 @@ class LeetCodeManager extends EventEmitter {
136186
} else if (data.match(this.failRegex)) {
137187
childProc.stdin?.end();
138188
return reject(new Error("Faile to login"));
189+
} else if (data.match(/login:/)){
190+
childProc.stdin?.write(`${name}\n`);
191+
} else if (data.match(/cookie:/)){
192+
childProc.stdin?.write(`${cookie}\n`);
139193
}
140194
});
141195

142196
childProc.stderr?.on("data", (data: string | Buffer) => leetCodeChannel.append(data.toString()));
143197

144198
childProc.on("error", reject);
145-
childProc.stdin?.write(`${name}\n`);
146-
await sleep(800);
147-
childProc.stdin?.write(`${cookie}\n`);
148199
});
149200
}
150201
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /