Skip to content

Commit 40cef4a

Browse files
committed
feat: re-add cookie-based login method
1 parent b495812 commit 40cef4a

File tree

1 file changed

+61
-16
lines changed

1 file changed

+61
-16
lines changed

Diff for: src/leetCodeManager.ts

+61-16
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,55 @@ 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 handleCookieSignIn(): Promise<void> {
78+
const cookie: string | undefined = await vscode.window.showInputBox({
79+
prompt: 'Enter cookie',
80+
password: true,
81+
ignoreFocusOut: true,
82+
validateInput: (s: string): string | undefined =>
83+
s ? undefined : 'Cookie must not be empty',
84+
})
85+
86+
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+
await this.handleCookieSignIn()
73117
}
74118

75119
public async signOut(): Promise<void> {
@@ -136,15 +180,16 @@ class LeetCodeManager extends EventEmitter {
136180
} else if (data.match(this.failRegex)) {
137181
childProc.stdin?.end();
138182
return reject(new Error("Faile to login"));
183+
} else if (data.match(/login: /)){
184+
childProc.stdin?.write(`${name}\n`);
185+
} else if (data.match(/cookie: /)){
186+
childProc.stdin?.write(`${cookie}\n`);
139187
}
140188
});
141189

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

144192
childProc.on("error", reject);
145-
childProc.stdin?.write(`${name}\n`);
146-
await sleep(800);
147-
childProc.stdin?.write(`${cookie}\n`);
148193
});
149194
}
150195
}

0 commit comments

Comments
 (0)