@@ -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 } ) ;
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 } ` ) ;
0 commit comments