Skip to content

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

Merged
merged 13 commits into from
Jan 12, 2020
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Thanks for [@yihong0618](https://github.com/yihong0618) provided a workaround wh
- **LeetCode: Sign in (by cookie)**
- **LeetCode: Sign out**

- You can also use the following command to sign in by third party:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please only reserve the common login command as it was before.

- **LeetCode: Sign in by GitHub**
- **LeetCode: Sign in by LinkedIn**
---

### Switch Endpoint
Expand Down
3 changes: 3 additions & 0 deletions docs/README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
- **LeetCode: Sign in (by cookie)**
- **LeetCode: Sign out**

- 你也可以使用下列命令第三方登入:
- **LeetCode: Sign in by GitHub**
- **LeetCode: Sign in by LinkedIn**
---

### 切换 LeetCode 版本
Expand Down
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,11 @@
"onCommand:leetcode.testSolution",
"onCommand:leetcode.submitSolution",
"onCommand:leetcode.switchDefaultLanguage",
"onCommand:leetcode.signinByCookie",
"onView:leetCodeExplorer"
],
"main": "./out/src/extension",
"contributes": {
"commands": [
{
"command": "leetcode.signinByCookie",
"title": "Sign In by Cookie",
"category": "LeetCode"
},
{
"command": "leetcode.deleteCache",
"title": "Delete Cache",
Expand Down
1 change: 0 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.commands.registerCommand("leetcode.deleteCache", () => cache.deleteCache()),
vscode.commands.registerCommand("leetcode.toggleLeetCodeCn", () => plugin.switchEndpoint()),
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()),
vscode.commands.registerCommand("leetcode.signinByCookie", () => leetCodeManager.signIn(true)),
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()),
vscode.commands.registerCommand("leetcode.manageSessions", () => session.manageSessions()),
vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)),
Expand Down
46 changes: 40 additions & 6 deletions src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, loginCommand, UserStatus } from "./shared";
import { createEnvOption } from "./utils/cpUtils";
import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";
import * as wsl from "./utils/wslUtils";
Expand Down Expand Up @@ -34,10 +34,44 @@ 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",
description: "",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to set the field description to empty string if you do not want to render it. Just remove it from the Object.

detail: "Use LeetCode account to login",
value: "LeetCode",
},
{
label: "LeetCode Cookie",
description: "",
detail: "Use LeetCode cookie that copy from browser to login",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use LeetCode cookie copied from browser to login

value: "Cookie",
},
{
label: "Third-Party: GitHub",
description: "",
detail: "Use third party GitHub account to login",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use GitHub account to login

value: "GitHub",
},
{
label: "Third-Party: LinkedIn",
description: "",
detail: "Use third party LinkedIn account to login",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 = loginCommand.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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this variable inMessage used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
e.g.

promptForOpenOutputChannel(Failed to ${inMessage}. Please open the output channel for details

try {
const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise<void> => {
Expand Down Expand Up @@ -82,7 +116,7 @@ class LeetCodeManager extends EventEmitter {
childProc.stdin.write(`${pwd}\n`);
childProc.stdin.end();
childProc.on("close", () => {
const match: RegExpMatchArray | null = result.match(/(?:.*) Successfully (login|cookie login) as (.*)/i);
const match: RegExpMatchArray | null = result.match(/(?:.*) Successfully (login|cookie login|third party login) as (.*)/i);
if (match && match[2]) {
resolve(match[2]);
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export enum UserStatus {
SignedOut = 2,
}

export const loginCommand: Map<string, string> = new Map([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loginCommand -> loginArgsMapping

["LeetCode", "-l"],
["Cookie", "-c"],
["GitHub", "-g"],
["LinkedIn", "-i"],
]);

export const languages: string[] = [
"bash",
"c",
Expand Down