Skip to content

Commit a80d18a

Browse files
author
Giovanni D'Andrea
committed
feat: created uri handler
- added uri handler to catch the token params from the Uri string - changed the workspace configuration to store token, before was only for a specific project, now is for specific User (the user who use vscode) - created a function to spawn an external browser
1 parent ad3fa61 commit a80d18a

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

codeishot_extension/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"categories": [
1616
"Other"
1717
],
18-
"activationEvents": [],
18+
"activationEvents": ["onUri"],
1919
"main": "./out/extension.js",
2020
"contributes": {
2121
"commands": [
@@ -47,6 +47,12 @@
4747
"when": "editorTextFocus"
4848
}
4949
],
50+
"uriHandler": [
51+
{
52+
"scheme": "codeishot.codeishot",
53+
"path": "/login"
54+
}
55+
],
5056
"configuration": {
5157
"type": "string",
5258
"title": "Codeishot",

codeishot_extension/src/extension.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from "vscode";
22
import * as path from "path";
3-
import { login } from "./login";
43
import { createSnippet } from "./services/codeishotServices";
4+
import { saveToken, login } from "./login";
55

66
const UI_BASE_URL: string = process.env.UI_BASE_URL || "https://codeishot.com";
77

@@ -146,6 +146,21 @@ function activate(context: vscode.ExtensionContext) {
146146

147147
context.subscriptions.push(disposable);
148148
context.subscriptions.push(loginCommand);
149+
context.subscriptions.push(
150+
// vscode://codeishot.codeishot/login?jwt=<token>
151+
// TODO: @Cleanup => Move to another file, maybe `handlers.ts`?
152+
vscode.window.registerUriHandler({
153+
handleUri(uri: vscode.Uri) {
154+
if (uri.path === "/login") {
155+
const jwtParam = uri.query.split("=")[1];
156+
if (jwtParam) {
157+
saveToken(jwtParam);
158+
vscode.window.showInformationMessage("Logged Successfully! ✨");
159+
}
160+
}
161+
},
162+
})
163+
);
149164
}
150165

151166
function deactivate() {}

codeishot_extension/src/handlers.ts

Whitespace-only changes.

codeishot_extension/src/login.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import { exit } from "process";
22
import * as vscode from "vscode";
33
import { verifyToken } from "./services/codeishotServices";
44

5-
// TODO: Move this to a configuration file
6-
const API_BASE_URL: string =
7-
process.env.API_BASE_URL || "https://api.codeishot.com";
8-
95
function getTokenFromConfiguration(): string {
106
let config = vscode.workspace.getConfiguration();
117
if (!config) {
@@ -24,10 +20,10 @@ function getTokenFromConfiguration(): string {
2420
return token;
2521
}
2622

27-
function saveToken(token: string) {
23+
export function saveToken(token: string) {
2824
vscode.workspace
2925
.getConfiguration()
30-
.update("jwt", token, vscode.ConfigurationTarget.Workspace);
26+
.update("jwt", token, vscode.ConfigurationTarget.Global);
3127
}
3228

3329
async function getTokenFromUser(): Promise<string> {
@@ -44,11 +40,16 @@ async function getTokenFromUser(): Promise<string> {
4440
return (searchQuery && searchQuery) || "";
4541
}
4642

47-
async function getToken() {
43+
async function openLoginBrowser() {
4844
// open the browser with google login and get the token
45+
// TODO: Change the url using .env
46+
vscode.env.openExternal(
47+
vscode.Uri.parse("http://localhost:3000/login?type=vscode")
48+
);
4949
}
5050

5151
async function isValidToken(): Promise<boolean> {
52+
// @Question => is this function really useful?
5253
let token = getTokenFromConfiguration();
5354
vscode.window.showInformationMessage(token);
5455

@@ -63,14 +64,7 @@ async function isValidToken(): Promise<boolean> {
6364
}
6465

6566
async function login() {
66-
vscode.window.showInformationMessage("Login To codeishot");
67-
let token = await getTokenFromUser();
68-
if (token !== "") {
69-
saveToken(token);
70-
if (await isValidToken())
71-
vscode.window.showInformationMessage("Logged Successfully! ✨");
72-
// TODO: Configure axios interceptor instance to use this token on requests
73-
}
67+
openLoginBrowser(); // spawn the codeishot login page
7468
}
7569

7670
export { login, getTokenFromConfiguration };

0 commit comments

Comments
 (0)