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 e40e646

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

File tree

1 file changed

+62
-16
lines changed

1 file changed

+62
-16
lines changed

‎src/leetCodeManager.ts‎

Lines changed: 62 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,56 @@ 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+
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+
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+
7190
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()
73118
}
74119

75120
public async signOut(): Promise<void> {
@@ -136,15 +181,16 @@ class LeetCodeManager extends EventEmitter {
136181
} else if (data.match(this.failRegex)) {
137182
childProc.stdin?.end();
138183
return reject(new Error("Faile to login"));
184+
} else if (data.match(/login:/)){
185+
childProc.stdin?.write(`${name}\n`);
186+
} else if (data.match(/cookie:/)){
187+
childProc.stdin?.write(`${cookie}\n`);
139188
}
140189
});
141190

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

144193
childProc.on("error", reject);
145-
childProc.stdin?.write(`${name}\n`);
146-
await sleep(800);
147-
childProc.stdin?.write(`${cookie}\n`);
148194
});
149195
}
150196
}

0 commit comments

Comments
(0)

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