Skip to content

Commit cab93a9

Browse files
committed
Merge branch 'feature/new-cli' of gitlab.com:postgres-ai/postgres_ai into feature/new-cli
2 parents 54a9829 + 6936145 commit cab93a9

File tree

4 files changed

+65
-21
lines changed

4 files changed

+65
-21
lines changed

cli/bin/postgres-ai.ts

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ program
685685
// Step 1: Start local callback server FIRST to get actual port
686686
console.log("Starting local callback server...");
687687
const requestedPort = opts.port || 0; // 0 = OS assigns available port
688-
const callbackServer = authServer.createCallbackServer(requestedPort, params.state, 300000);
688+
const callbackServer = authServer.createCallbackServer(requestedPort, params.state, 120000); // 2 minute timeout
689689

690690
// Wait a bit for server to start and get port
691691
await new Promise(resolve => setTimeout(resolve, 100));
@@ -727,10 +727,20 @@ program
727727
res.on("end", async () => {
728728
if (res.statusCode !== 200) {
729729
console.error(`Failed to initialize auth session: ${res.statusCode}`);
730-
console.error(data);
730+
731+
// Check if response is HTML (common for 404 pages)
732+
if (data.trim().startsWith("<!") || data.trim().startsWith("<html")) {
733+
console.error("Error: Received HTML response instead of JSON. This usually means:");
734+
console.error(" 1. The API endpoint URL is incorrect");
735+
console.error(" 2. The endpoint does not exist (404)");
736+
console.error(`\nAPI URL attempted: ${initUrl.toString()}`);
737+
console.error("\nPlease verify the --api-base-url parameter.");
738+
} else {
739+
console.error(data);
740+
}
741+
731742
callbackServer.server.close();
732-
process.exitCode = 1;
733-
return;
743+
process.exit(1);
734744
}
735745

736746
// Step 3: Open browser
@@ -751,9 +761,22 @@ program
751761

752762
// Step 4: Wait for callback
753763
console.log("Waiting for authorization...");
764+
console.log("(Press Ctrl+C to cancel)\n");
765+
766+
// Handle Ctrl+C gracefully
767+
const cancelHandler = () => {
768+
console.log("\n\nAuthentication cancelled by user.");
769+
callbackServer.server.close();
770+
process.exit(130); // Standard exit code for SIGINT
771+
};
772+
process.on("SIGINT", cancelHandler);
773+
754774
try {
755775
const { code } = await callbackServer.promise;
756776

777+
// Remove the cancel handler after successful auth
778+
process.off("SIGINT", cancelHandler);
779+
757780
// Step 5: Exchange code for token
758781
console.log("\nExchanging authorization code for API token...");
759782
const exchangeData = JSON.stringify({
@@ -777,7 +800,18 @@ program
777800
exchangeRes.on("end", () => {
778801
if (exchangeRes.statusCode !== 200) {
779802
console.error(`Failed to exchange code for token: ${exchangeRes.statusCode}`);
780-
console.error(exchangeBody);
803+
804+
// Check if response is HTML (common for 404 pages)
805+
if (exchangeBody.trim().startsWith("<!") || exchangeBody.trim().startsWith("<html")) {
806+
console.error("Error: Received HTML response instead of JSON. This usually means:");
807+
console.error(" 1. The API endpoint URL is incorrect");
808+
console.error(" 2. The endpoint does not exist (404)");
809+
console.error(`\nAPI URL attempted: ${exchangeUrl.toString()}`);
810+
console.error("\nPlease verify the --api-base-url parameter.");
811+
} else {
812+
console.error(exchangeBody);
813+
}
814+
781815
process.exit(1);
782816
return;
783817
}
@@ -817,8 +851,20 @@ program
817851
exchangeReq.end();
818852

819853
} catch (err) {
854+
// Remove the cancel handler in error case too
855+
process.off("SIGINT", cancelHandler);
856+
820857
const message = err instanceof Error ? err.message : String(err);
821-
console.error(`\nAuthentication failed: ${message}`);
858+
859+
// Provide more helpful error messages
860+
if (message.includes("timeout")) {
861+
console.error(`\nAuthentication timed out.`);
862+
console.error(`This usually means you closed the browser window without completing authentication.`);
863+
console.error(`Please try again and complete the authentication flow.`);
864+
} else {
865+
console.error(`\nAuthentication failed: ${message}`);
866+
}
867+
822868
process.exit(1);
823869
}
824870
});
@@ -953,8 +999,8 @@ mon
953999
console.log(" URL: http://localhost:3000");
9541000
console.log(" Username: monitor");
9551001
console.log(` Password: ${newPassword}`);
956-
console.log("\nRestart Grafana to apply:");
957-
console.log(" postgres-ai mon restart grafana");
1002+
console.log("\nReset Grafana to apply new password:");
1003+
console.log(" postgres-ai mon reset grafana");
9581004
} catch (error) {
9591005
const message = error instanceof Error ? error.message : String(error);
9601006
console.error(`Failed to generate password: ${message}`);

cli/lib/auth-server.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export function createCallbackServer(
9797
<!DOCTYPE html>
9898
<html>
9999
<head>
100-
<title>Authentication Failed</title>
100+
<title>Authentication failed</title>
101101
<style>
102102
body { font-family: system-ui, -apple-system, sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; }
103103
.error { background: #fee; border: 1px solid #fcc; padding: 20px; border-radius: 8px; }
@@ -107,7 +107,7 @@ export function createCallbackServer(
107107
</head>
108108
<body>
109109
<div class="error">
110-
<h1>Authentication Failed</h1>
110+
<h1>Authentication failed</h1>
111111
<p><strong>Error:</strong> ${escapeHtml(error)}</p>
112112
${errorDescription ? `<p><strong>Description:</strong> ${escapeHtml(errorDescription)}</p>` : ""}
113113
<p>You can close this window and return to your terminal.</p>
@@ -130,7 +130,7 @@ export function createCallbackServer(
130130
<!DOCTYPE html>
131131
<html>
132132
<head>
133-
<title>Authentication Failed</title>
133+
<title>Authentication failed</title>
134134
<style>
135135
body { font-family: system-ui, -apple-system, sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; }
136136
.error { background: #fee; border: 1px solid #fcc; padding: 20px; border-radius: 8px; }
@@ -139,7 +139,7 @@ export function createCallbackServer(
139139
</head>
140140
<body>
141141
<div class="error">
142-
<h1>Authentication Failed</h1>
142+
<h1>Authentication failed</h1>
143143
<p>Missing required parameters (code or state).</p>
144144
<p>You can close this window and return to your terminal.</p>
145145
</div>
@@ -159,7 +159,7 @@ export function createCallbackServer(
159159
<!DOCTYPE html>
160160
<html>
161161
<head>
162-
<title>Authentication Failed</title>
162+
<title>Authentication failed</title>
163163
<style>
164164
body { font-family: system-ui, -apple-system, sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; }
165165
.error { background: #fee; border: 1px solid #fcc; padding: 20px; border-radius: 8px; }
@@ -168,7 +168,7 @@ export function createCallbackServer(
168168
</head>
169169
<body>
170170
<div class="error">
171-
<h1>Authentication Failed</h1>
171+
<h1>Authentication failed</h1>
172172
<p>Invalid state parameter (possible CSRF attack).</p>
173173
<p>You can close this window and return to your terminal.</p>
174174
</div>
@@ -192,7 +192,7 @@ export function createCallbackServer(
192192
<!DOCTYPE html>
193193
<html>
194194
<head>
195-
<title>Authentication Successful</title>
195+
<title>Authentication successful</title>
196196
<style>
197197
body { font-family: system-ui, -apple-system, sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; }
198198
.success { background: #efe; border: 1px solid #cfc; padding: 20px; border-radius: 8px; }
@@ -201,7 +201,7 @@ export function createCallbackServer(
201201
</head>
202202
<body>
203203
<div class="success">
204-
<h1>Authentication Successful</h1>
204+
<h1>Authentication successful</h1>
205205
<p>You have successfully authenticated the PostgresAI CLI.</p>
206206
<p>You can close this window and return to your terminal.</p>
207207
</div>

cli/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postgresai",
3-
"version": "0.11.0-alpha.11",
3+
"version": "0.11.0-alpha.12",
44
"description": "postgres_ai CLI (Node.js)",
55
"license": "Apache-2.0",
66
"private": false,
@@ -40,5 +40,3 @@
4040
"access": "public"
4141
}
4242
}
43-
44-

0 commit comments

Comments
 (0)