Skip to content

Commit 4721c3f

Browse files
authored
feat: OAuth (#47)
* feat: changes for OAuth flow * fix: reuse existing panel * docs: update README for auth dialogs
1 parent 7b32739 commit 4721c3f

File tree

5 files changed

+45
-20
lines changed

5 files changed

+45
-20
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ After installing the extension,
4141
1. Click on the newly-added Source Academy icon on the left sidebar.
4242
2. Launch the Source Academy panel.
4343

44-
<img src="./docs/images/launch-panel-from-sidebar.png" width="600">
44+
<img src="./docs/images/launch-panel-from-sidebar.png" width="600">
4545

46-
Alternatively, use the `Source Academy: Show the Source Academy Panel` command.
46+
Alternatively, use the `Source Academy: Show the Source Academy Panel` command.
4747

48-
Once the panel loads, you'll be prompted to log in. Afterwards, begin coding by opening any assessment.
48+
3. Choose a login method and authenticate in your browser.
49+
50+
- When prompted, click **Open** to proceed to the external website.
51+
- After login, click **Open** again to allow the extension to open the URI. Optionally, check `Do not ask me again for this extension`.
52+
53+
4. You may now begin coding by opening any assessment.
4954

5055
### Changing the frontend
5156

src/commands/showPanel.tsx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@ export async function showPanel(
2222
language = LANGUAGES.SOURCE_1;
2323
}
2424

25-
// Get a reference to the active editor (before the focus is switched to our newly created webview)
26-
// firstEditor = vscode.window.activeTextEditor!;
27-
28-
messageHandler.panel = vscode.window.createWebviewPanel(
29-
"source-academy-panel",
30-
"Source Academy",
31-
vscode.ViewColumn.Beside,
32-
{
33-
enableScripts: true, // Enable scripts in the webview
34-
retainContextWhenHidden: true,
35-
},
36-
);
25+
// Don't recreate the panel it already exists. There will only be one panel at anytime.
26+
if (!messageHandler.panel) {
27+
messageHandler.panel = vscode.window.createWebviewPanel(
28+
"source-academy-panel",
29+
"Source Academy",
30+
vscode.ViewColumn.Beside,
31+
{
32+
enableScripts: true, // Enable scripts in w the webview
33+
retainContextWhenHidden: true,
34+
},
35+
);
3736

38-
messageHandler.panel.webview.onDidReceiveMessage(
39-
(message: MessageType) => messageHandler.handleMessage(context, message),
40-
undefined,
41-
context.subscriptions,
42-
);
37+
messageHandler.panel.webview.onDidReceiveMessage(
38+
(message: MessageType) => messageHandler.handleMessage(context, message),
39+
undefined,
40+
context.subscriptions,
41+
);
42+
}
4343

4444
const iframeUrl = new URL(route ?? "/playground", config.frontendBaseUrl)
4545
.href;

src/extension.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ export function activate(context: vscode.ExtensionContext) {
5151
"*.js": "source",
5252
});
5353
}
54+
55+
vscode.window.registerUriHandler({
56+
handleUri(uri: vscode.Uri) {
57+
const searchParams = new URLSearchParams(uri.query);
58+
// The following two params are available when logging in via OAuth providers
59+
const code = searchParams.get("code");
60+
const clientRequestId = searchParams.get("client-request-id");
61+
62+
vscode.commands.executeCommand(
63+
"source-academy.show-panel",
64+
`/login/vscode_callback?code=${code}&client-request-id=${clientRequestId}`,
65+
);
66+
},
67+
});
5468
}
5569

5670
// This method is called when your extension is deactivated

src/utils/messageHandler.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ export class MessageHandler {
173173
this.panel?.reveal(vscode.ViewColumn.Two);
174174
}
175175
break;
176+
case MessageTypeNames.LoginWithBrowser:
177+
const { route } = message;
178+
vscode.env.openExternal(vscode.Uri.parse(route));
176179
}
177180
console.log(`${Date.now()} Finish handleMessage: ${message.type}`);
178181
}

src/utils/messages.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ const Messages = createMessages({
8989
questionId,
9090
choice,
9191
}),
92+
LoginWithBrowser: (route: string) => ({
93+
route,
94+
}),
9295
});
9396

9497
export default Messages;

0 commit comments

Comments
 (0)