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

Commit eb61e71

Browse files
authored
use /api/api-key/verify to verify existing api key and login as needed (#103)
1 parent 2ea769c commit eb61e71

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/commands/login/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { URL } from "node:url";
1010
import open from "open";
1111

1212
import { sendGetOrgsReq } from "../../util/graphql.js";
13-
import { writeToSettingsFile, promptOrgSelection } from "../../util/index.js";
13+
import { writeToSettingsFile, promptOrgSelection, readSettingsJson, getSettingsFilePath } from "../../util/index.js";
1414

1515
const loginHTML = `<!-- src/commands/login/login.html -->
1616
<!DOCTYPE html>
@@ -80,6 +80,31 @@ export default class LoginIndex extends Command {
8080
}
8181

8282
public async run(): Promise<void> {
83+
const settings = await readSettingsJson(getSettingsFilePath());
84+
const apiKey = settings.apiKey;
85+
86+
if (apiKey) {
87+
const verifyResponse = await fetch("https://hypermode.com/api/api-key/verify", {
88+
method: "POST",
89+
headers: {
90+
"Content-Type": "application/json",
91+
},
92+
body: JSON.stringify({
93+
key: apiKey,
94+
}),
95+
});
96+
97+
const verifyResponseJson = await verifyResponse.json();
98+
99+
if (verifyResponseJson.valid) {
100+
this.log("You are already logged in as " + chalk.dim(settings.email) + "! 🎉");
101+
return;
102+
}
103+
if (verifyResponseJson.error) {
104+
this.log("Your API key is invalid or expired. Please log in again.");
105+
}
106+
}
107+
83108
return new Promise((resolve, reject) => {
84109
const server = http.createServer(async (req, res) => {
85110
try {

0 commit comments

Comments
 (0)