Skip to content
This repository was archived by the owner on Aug 21, 2025. It is now read-only.

Commit 12a6c4c

Browse files
committed
use apiKey for requests
1 parent 85c064b commit 12a6c4c

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

src/commands/login/index.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default class LoginIndex extends Command {
7575

7676
public async openLoginPage() {
7777
// Open the Hypermode sign-in page in the default browser
78-
const loginUrl = "https://hypermode.com/app/callback?port=5051&type=cli";
78+
const loginUrl = "http://localhost:3000/callback?port=5051&type=cli";
7979
await open(loginUrl);
8080
}
8181

@@ -86,13 +86,35 @@ export default class LoginIndex extends Command {
8686
const url = new URL(req.url ?? "", `http://${req.headers.host}`);
8787
const jwt = url.searchParams.get("jwt");
8888
const email = url.searchParams.get("email");
89+
const userId = url.searchParams.get("user");
8990

90-
if (!jwt || !email) {
91+
if (!jwt || !email || !userId) {
9192
res.writeHead(400, { "Content-Type": "text/plain" });
9293
res.end("JWT or email not found in the request.");
9394
return;
9495
}
9596

97+
const response = await fetch("http://localhost:3000/api/api-key/create", {
98+
method: "POST",
99+
headers: {
100+
Authorization: `Bearer ${jwt}`,
101+
"Content-Type": "application/json",
102+
},
103+
body: JSON.stringify({
104+
userId,
105+
name: "CLI Access Key",
106+
expiresIn: 60 * 60 * 24 * 7, // 7 days
107+
prefix: "cli",
108+
}),
109+
});
110+
111+
const { data, error } = await response.json();
112+
const apiKey = data.key;
113+
114+
if (!apiKey || error) {
115+
throw new Error(error);
116+
}
117+
96118
res.writeHead(200, { "Content-Type": "text/html" });
97119
res.end(loginHTML);
98120

@@ -107,9 +129,9 @@ export default class LoginIndex extends Command {
107129
}
108130

109131
try {
110-
const orgs = await sendGetOrgsReq(jwt);
132+
const orgs = await sendGetOrgsReq(apiKey);
111133
const selectedOrg = await promptOrgSelection(orgs);
112-
await writeToSettingsFile(jwt, email, selectedOrg.id);
134+
await writeToSettingsFile(apiKey, email, selectedOrg.id);
113135
this.log("Successfully logged in as " + chalk.dim(email) + "! 🎉");
114136
resolve();
115137
} catch (error) {

src/util/graphql.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { Org, Project } from "../util/types.js";
77
import { getSlugFromName } from "./index.js";
88
import chalk from "chalk";
99

10-
export async function sendGraphQLReqToHypermode(jwt: string, query: string): Promise<any> {
11-
const url = "https://api.hypermode.com/graphql";
10+
export async function sendGraphQLReqToHypermode(apiKey: string, query: string): Promise<any> {
11+
const url = "http://localhost:9081/graphql";
1212

1313
const options = {
1414
body: JSON.stringify({ query }),
1515
headers: {
16-
Authorization: `${jwt}`,
16+
"X-API-Key": apiKey,
1717
"Content-Type": "application/json",
1818
},
1919
method: "POST",
@@ -25,7 +25,7 @@ export async function sendGraphQLReqToHypermode(jwt: string, query: string): Pro
2525
if (!response.ok) {
2626
if (response.status === 401) {
2727
console.error(`Unauthorized. Please try ${chalk.blueBright("hyp login")} again.`);
28-
throw new Error("Unauthorized: Invalid or expired JWT token.");
28+
throw new Error("Unauthorized: Invalid or expired API key.");
2929
} else {
3030
throw new Error(`HTTP Error: ${response.status} ${response.statusText}`);
3131
}
@@ -38,7 +38,7 @@ export async function sendGraphQLReqToHypermode(jwt: string, query: string): Pro
3838
}
3939
}
4040

41-
export async function sendMapRepoAndFinishProjectCreationReq(jwt: string, id: string, repoId: string, repoName: string): Promise<Project> {
41+
export async function sendMapRepoAndFinishProjectCreationReq(apiKey: string, id: string, repoId: string, repoName: string): Promise<Project> {
4242
const query = `
4343
mutation MapRepoAndFinishProjectCreation {
4444
mapRepoAndFinishProjectCreation(input: {id: "${id}", repoName: "${repoName}", repoId: "${repoId}", sourceType: CUSTOM, defaultBranchName: "main"}) {
@@ -48,14 +48,14 @@ export async function sendMapRepoAndFinishProjectCreationReq(jwt: string, id: st
4848
}
4949
}`;
5050

51-
const data: any = await sendGraphQLReqToHypermode(jwt, query);
51+
const data: any = await sendGraphQLReqToHypermode(apiKey, query);
5252

5353
const project: Project = data.data.mapRepoAndFinishProjectCreation;
5454

5555
return project;
5656
}
5757

58-
export async function sendCreateProjectReq(jwt: string, orgId: string, projectName: string, repoId: string, repoName: string): Promise<Project> {
58+
export async function sendCreateProjectReq(apiKey: string, orgId: string, projectName: string, repoId: string, repoName: string): Promise<Project> {
5959
const slug = getSlugFromName(projectName);
6060
const query = `
6161
mutation CreateProjectBranchRuntime {
@@ -67,14 +67,14 @@ export async function sendCreateProjectReq(jwt: string, orgId: string, projectNa
6767
}
6868
}`;
6969

70-
const res: any = await sendGraphQLReqToHypermode(jwt, query);
70+
const res: any = await sendGraphQLReqToHypermode(apiKey, query);
7171

7272
const project: Project = res.data.createProjectBranchRuntime;
7373

7474
return project;
7575
}
7676

77-
export async function sendGetOrgsReq(jwt: string): Promise<Org[]> {
77+
export async function sendGetOrgsReq(apiKey: string): Promise<Org[]> {
7878
const query = `
7979
query GetOrgs {
8080
getOrgs {
@@ -83,14 +83,14 @@ export async function sendGetOrgsReq(jwt: string): Promise<Org[]> {
8383
}
8484
}`;
8585

86-
const data: any = await sendGraphQLReqToHypermode(jwt, query);
86+
const data: any = await sendGraphQLReqToHypermode(apiKey, query);
8787

8888
const orgs: Org[] = data.data.getOrgs;
8989

9090
return orgs;
9191
}
9292

93-
export async function getProjectsByOrgReq(jwt: string, orgId: string): Promise<Project[]> {
93+
export async function getProjectsByOrgReq(apiKey: string, orgId: string): Promise<Project[]> {
9494
const query = `
9595
query GetProjectsByOrg {
9696
getOrg(id: "${orgId}") {
@@ -103,20 +103,20 @@ export async function getProjectsByOrgReq(jwt: string, orgId: string): Promise<P
103103
}
104104
}`;
105105

106-
const data: any = await sendGraphQLReqToHypermode(jwt, query);
106+
const data: any = await sendGraphQLReqToHypermode(apiKey, query);
107107

108108
const projects: Project[] = data.data.getOrg.projects;
109109

110110
return projects;
111111
}
112112

113-
export async function sendGetRepoIdReq(jwt: string, installationId: string, gitUrl: string): Promise<string> {
113+
export async function sendGetRepoIdReq(apiKey: string, installationId: string, gitUrl: string): Promise<string> {
114114
const query = `
115115
query getUserRepoIdByUrl {
116116
getUserRepoIdByUrl(installationId: "${installationId}", gitUrl: "${gitUrl}")
117117
}`;
118118

119-
const res: any = await sendGraphQLReqToHypermode(jwt, query);
119+
const res: any = await sendGraphQLReqToHypermode(apiKey, query);
120120

121121
if (!res.data.getUserRepoIdByUrl) {
122122
throw new Error("No repoId found for the given installationId and gitUrl");

src/util/index.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export async function readSettingsJson(filePath: string): Promise<{ content: str
159159
};
160160
}
161161

162-
export async function writeToSettingsFile(jwt: string, email: string, orgId: string): Promise<void> {
162+
export async function writeToSettingsFile(apiKey: string, email: string, orgId: string): Promise<void> {
163163
const settingsDir = getSettingsDir();
164164
const settingsFilePath = getSettingsFilePath();
165165

@@ -168,9 +168,9 @@ export async function writeToSettingsFile(jwt: string, email: string, orgId: str
168168
await fs.mkdir(settingsDir, { recursive: true });
169169
}
170170

171-
const newSettingsContent: { HYP_EMAIL: string; HYP_JWT: string; HYP_ORG_ID: string; INSTALLATION_IDS: { [key: string]: string } | null } = {
171+
const newSettingsContent: { HYP_EMAIL: string; HYP_API_KEY: string; HYP_ORG_ID: string; INSTALLATION_IDS: { [key: string]: string } | null } = {
172172
HYP_EMAIL: email,
173-
HYP_JWT: jwt,
173+
HYP_API_KEY: apiKey,
174174
HYP_ORG_ID: orgId,
175175
INSTALLATION_IDS: null,
176176
};
@@ -191,12 +191,7 @@ export async function writeGithubInstallationIdToSettingsFile(gitOwner: string,
191191
settings.installationIds = settings.installationIds || {};
192192
settings.installationIds[gitOwner] = installationId;
193193

194-
const newSettingsContent = {
195-
HYP_EMAIL: settings.email,
196-
HYP_JWT: settings.jwt,
197-
HYP_ORG_ID: settings.orgId,
198-
INSTALLATION_IDS: settings.installationIds,
199-
};
194+
const newSettingsContent = { ...settings };
200195

201196
await fs.writeFile(settingsFilePath, JSON.stringify(newSettingsContent, null, 2), { flag: "w" });
202197
}

0 commit comments

Comments
 (0)