Skip to content

Commit eb2b07c

Browse files
can interactively sign in
1 parent b6cd230 commit eb2b07c

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

src/commands/user.ts

+50-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22

3+
import * as cp from "child_process";
34
import * as vscode from "vscode";
5+
import { leetcodeChannel } from "../leetCodeChannel";
46
import { leetCodeBinaryPath } from "../shared";
57
import { executeCommand } from "../utils/cpUtils";
68
import { DialogOptions, DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
@@ -22,16 +24,60 @@ export async function getSignedInAccount(): Promise<string | undefined> {
2224
}
2325
}
2426

25-
export function signIn(terminal: vscode.Terminal): void {
26-
terminal.show();
27-
terminal.sendText(`node ${leetCodeBinaryPath} user -l`);
27+
export async function signIn(): Promise<void> {
28+
// work around for interactive login
29+
try {
30+
await new Promise(async (resolve: (res: string) => void, reject: (e: Error) => void): Promise<void> => {
31+
let result: string = "";
32+
const childProc: cp.ChildProcess = cp.spawn("node", [leetCodeBinaryPath, "user", "-l"]);
33+
childProc.stdout.on("data", (data: string | Buffer) => {
34+
data = data.toString();
35+
result = result.concat(data);
36+
leetcodeChannel.append(data);
37+
});
38+
39+
childProc.stderr.on("data", (data: string | Buffer) => leetcodeChannel.append(data.toString()));
40+
41+
childProc.on("error", reject);
42+
childProc.on("exit", (code: number) => {
43+
if (code !== 0 || result.indexOf("ERROR") > -1) {
44+
reject(new Error("Login failed"));
45+
} else {
46+
resolve(result);
47+
}
48+
});
49+
const user: string | undefined = await vscode.window.showInputBox({
50+
prompt: "Enter user name.",
51+
validateInput: (s: string) => s ? undefined : "User name must not be empty",
52+
});
53+
if (!user) {
54+
childProc.kill();
55+
reject(new Error("Login Cancelled"));
56+
}
57+
childProc.stdin.write(`${user}\n`);
58+
const pwd: string | undefined = await vscode.window.showInputBox({
59+
prompt: "Enter user name.",
60+
password: true,
61+
validateInput: (s: string) => s ? undefined : "Password must not be empty",
62+
});
63+
if (!pwd) {
64+
childProc.kill();
65+
reject(new Error("Login Cancelled"));
66+
}
67+
childProc.stdin.write(`${pwd}\n`);
68+
childProc.stdin.end();
69+
});
70+
vscode.window.showInformationMessage("Successfully signed in.");
71+
} catch (error) {
72+
await promptForOpenOutputChannel("Failed to sign in. Please open the output channel for details", DialogType.error);
73+
}
2874
}
2975

3076
export async function signOut(): Promise<void> {
3177
try {
3278
await executeCommand("node", [leetCodeBinaryPath, "user", "-L"]);
3379
vscode.window.showInformationMessage("Successfully signed out.");
3480
} catch (error) {
35-
await promptForOpenOutputChannel("Failed to sign out. Would you like to open output channel for detais?", DialogType.error);
81+
await promptForOpenOutputChannel("Failed to sign out. Please open the output channel for details", DialogType.error);
3682
}
3783
}

src/extension.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function activate(context: vscode.ExtensionContext) {
77
const terminal: vscode.Terminal = vscode.window.createTerminal("LeetCode");
88
context.subscriptions.push(
99
terminal,
10-
vscode.commands.registerCommand("leetcode.signin", () => user.signIn(terminal)),
10+
vscode.commands.registerCommand("leetcode.signin", () => user.signIn()),
1111
vscode.commands.registerCommand("leetcode.signout", () => user.signOut()),
1212
);
1313
}

src/leetCodeTerminal.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)