-
Notifications
You must be signed in to change notification settings - Fork 677
add thrid party login -- GitHub and LinkedIn #496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
af76a8b
3124790
6ab8a3b
8d24d57
ae2fa04
ed51cb5
e9fbd35
e4606fc
ae9463e
4b0577e
f52cde3
a7f2ff0
b05f9d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import { EventEmitter } from "events"; | |
import * as vscode from "vscode"; | ||
import { leetCodeChannel } from "./leetCodeChannel"; | ||
import { leetCodeExecutor } from "./leetCodeExecutor"; | ||
import { UserStatus } from "./shared"; | ||
import { IQuickItemEx, loginArgsMapping, UserStatus } from "./shared"; | ||
import { createEnvOption } from "./utils/cpUtils"; | ||
import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils"; | ||
import * as wsl from "./utils/wslUtils"; | ||
|
@@ -34,14 +34,43 @@ class LeetCodeManager extends EventEmitter { | |
} | ||
} | ||
|
||
public async signIn(isByCookie: boolean = false): Promise<void> { | ||
const loginArg: string = "-l"; | ||
const cookieArg: string = "-c"; | ||
const commandArg: string = isByCookie ? cookieArg : loginArg; | ||
public async signIn(): Promise<void> { | ||
const picks: Array<IQuickItemEx<string>> = []; | ||
picks.push( | ||
{ | ||
label: "LeetCode Account", | ||
detail: "Use LeetCode account to login", | ||
value: "LeetCode", | ||
}, | ||
{ | ||
label: "LeetCode Cookie", | ||
detail: "Use LeetCode cookie copied from browser to login", | ||
value: "Cookie", | ||
}, | ||
{ | ||
label: "Third-Party: GitHub", | ||
detail: "Use GitHub account to login", | ||
value: "GitHub", | ||
}, | ||
{ | ||
label: "Third-Party: LinkedIn", | ||
detail: "Use LinkedIn account to login", | ||
value: "LinkedIn", | ||
}, | ||
); | ||
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(picks); | ||
if (!choice) { | ||
return; | ||
} | ||
const loginMethod: string = choice.value; | ||
const commandArg: string | undefined = loginArgsMapping.get(loginMethod); | ||
if (!commandArg) { | ||
throw new Error(`The login method "${loginMethod}" is not supported.`); | ||
} | ||
const isByCookie: boolean = loginMethod === "Cookie"; | ||
const inMessage: string = isByCookie ? "sign in by cookie" : "sign in"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is for the difference of cookie login or other login message.
|
||
try { | ||
const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise<void> => { | ||
let result: string = ""; | ||
|
||
const leetCodeBinaryPath: string = await leetCodeExecutor.getLeetCodeBinaryPath(); | ||
|
||
|
@@ -52,10 +81,27 @@ class LeetCodeManager extends EventEmitter { | |
env: createEnvOption(), | ||
}); | ||
|
||
childProc.stdout.on("data", (data: string | Buffer) => { | ||
childProc.stdout.on("data", async (data: string | Buffer) => { | ||
data = data.toString(); | ||
result = result.concat(data); | ||
leetCodeChannel.append(data); | ||
if (data.includes("twoFactorCode")) { | ||
const twoFactor: string | undefined = await vscode.window.showInputBox({ | ||
prompt: "Enter two-factor code.", | ||
validateInput: (s: string): string | undefined => s && s.trim() ? undefined : "The input must not be empty", | ||
}); | ||
if (!twoFactor) { | ||
childProc.kill(); | ||
return resolve(undefined); | ||
} | ||
childProc.stdin.write(`${twoFactor}\n`); | ||
childProc.stdin.end(); | ||
} else { | ||
const match: RegExpMatchArray | null = data.match(/(?:.*)Successfully .*login as (.*)/i); | ||
if (match && match[1]) { | ||
childProc.stdin.end(); | ||
return resolve(match[1]); | ||
} | ||
} | ||
}); | ||
|
||
childProc.stderr.on("data", (data: string | Buffer) => leetCodeChannel.append(data.toString())); | ||
|
@@ -80,13 +126,9 @@ class LeetCodeManager extends EventEmitter { | |
return resolve(undefined); | ||
} | ||
childProc.stdin.write(`${pwd}\n`); | ||
childProc.stdin.end(); | ||
childProc.on("close", () => { | ||
const match: RegExpMatchArray | null = result.match(/(?:.*) Successfully (login|cookie login) as (.*)/i); | ||
if (match && match[2]) { | ||
resolve(match[2]); | ||
} else { | ||
reject(new Error(`Failed to ${inMessage}.`)); | ||
childProc.on("close", (code: number) => { | ||
if (code !== 0) { | ||
reject(new Error("Failed to login.")); | ||
} | ||
}); | ||
}); | ||
|