From 210fd0206ec093b9fa5dd234246d3982bce6f2df Mon Sep 17 00:00:00 2001 From: Matt Wilde Date: Thu, 16 Jan 2025 09:05:59 -0500 Subject: [PATCH 1/2] fix loopback server port for login --- src/commands/login.mjs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/commands/login.mjs b/src/commands/login.mjs index 1eb253c9..35a89550 100644 --- a/src/commands/login.mjs +++ b/src/commands/login.mjs @@ -38,22 +38,29 @@ async function doLogin(argv) { logger.stderr(err); } }; - const authCodeParams = oAuth.getOAuthParams({ - clientId: argv.clientId, - noRedirect: argv.noRedirect, - }); - const dashboardOAuthURL = await startOAuthRequest(authCodeParams); - logger.stdout(`To login, open a browser to:\n${dashboardOAuthURL}`); + + const promptLoginUrl = async () => { + const authCodeParams = oAuth.getOAuthParams({ + clientId: argv.clientId, + noRedirect: argv.noRedirect, + }); + const dashboardOAuthURL = await startOAuthRequest(authCodeParams); + logger.stdout(`To login, open a browser to:\n${dashboardOAuthURL}`); + return dashboardOAuthURL; + }; + if (!argv.noRedirect) { oAuth.server.on("ready", async () => { + const dashboardOAuthURL = await promptLoginUrl(); open(dashboardOAuthURL); + logger.stdout("Waiting for authentication in browser to complete..."); }); oAuth.server.on("auth_code_received", async () => { await loginWithToken(); }); await oAuth.start(); - logger.stdout("Waiting for authentication in browser to complete..."); } else { + await promptLoginUrl(); try { const userCode = await input({ message: "Authorization Code:", From 715a3b88fc7f1f95a7a4d98469c55c0ad0e442a8 Mon Sep 17 00:00:00 2001 From: Matt Wilde Date: Thu, 16 Jan 2025 09:19:14 -0500 Subject: [PATCH 2/2] verify in test that server starts before we get the redirect uri --- test/commands/login.mjs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/commands/login.mjs b/test/commands/login.mjs index e76c4449..8222634c 100644 --- a/test/commands/login.mjs +++ b/test/commands/login.mjs @@ -111,6 +111,8 @@ describe("login", function () { // We start the loopback server expect(oauthClient.start.called).to.be.true; + expect(oauthClient.start.calledBefore(oauthClient.getOAuthParams)).to.be + .true; // We open auth url in the browser and prompt user expect(container.resolve("open").calledWith("http://dashboard-url.com")); expect(logger.stdout).to.have.been.calledWith(