From b78c4951ae0b73f769e886a264c5ef7284f02eda Mon Sep 17 00:00:00 2001 From: Mounib Date: Wed, 16 Nov 2022 20:01:28 +0100 Subject: [PATCH 1/2] feat: support targeting browser in login command --- command-snapshot.json | 3 ++- messages/web.login.json | 6 ++++-- src/commands/auth/web/login.ts | 11 ++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/command-snapshot.json b/command-snapshot.json index d9f86f10..eb634642 100644 --- a/command-snapshot.json +++ b/command-snapshot.json @@ -83,7 +83,8 @@ "noprompt", "setalias", "setdefaultdevhubusername", - "setdefaultusername" + "setdefaultusername", + "browser" ], "alias": ["force:auth:web:login"] } diff --git a/messages/web.login.json b/messages/web.login.json index 339fb4e2..f765b91d 100644 --- a/messages/web.login.json +++ b/messages/web.login.json @@ -1,10 +1,12 @@ { - "description": "authorize an org using the web login flow\nIf you specify an --instanceurl value, this value overrides the sfdcLoginUrl value in your sfdx-project.json file. To specify a My Domain URL, use the format MyDomainName.my.salesforce.com (not MyDomainName.lightning.force.com). To log in to a sandbox, set --instanceurl to https://MyDomainName--SandboxName.sandbox.my.salesforce.com.", + "description": "authorize an org using the web login flow\nIf you specify an --instanceurl value, this value overrides the sfdcLoginUrl value in your sfdx-project.json file. To specify a My Domain URL, use the format MyDomainName.my.salesforce.com (not MyDomainName.lightning.force.com). To log in to a sandbox, set --instanceurl to https://MyDomainName--SandboxName.sandbox.my.salesforce.com.\nTo open in a specific browser, use the --browser parameter. Supported browsers are \"chrome\", \"edge\", and \"firefox\". If you don't specify --browser, the org opens in your default browser.", "examples": [ "$ sfdx auth:web:login -a TestOrg1", "$ sfdx auth:web:login -i ", - "$ sfdx auth:web:login -r https://MyDomainName--SandboxName.sandbox.my.salesforce.com" + "$ sfdx auth:web:login -r https://MyDomainName--SandboxName.sandbox.my.salesforce.com", + "sfdx auth:web:login -a TestOrg1 -b firefox" ], + "browser": "browser where the org opens", "deviceWarning": "auth:web:login doesn't work when authorizing to a headless environment. Use auth:device:login instead.", "invalidClientId": "Invalid client credentials. Verify the OAuth client secret and ID. %s" } diff --git a/src/commands/auth/web/login.ts b/src/commands/auth/web/login.ts index faaf0e44..5d689b4b 100644 --- a/src/commands/auth/web/login.ts +++ b/src/commands/auth/web/login.ts @@ -26,6 +26,12 @@ export default class Login extends SfdxCommand { public static aliases = ['force:auth:web:login']; public static readonly flagsConfig: FlagsConfig = { + browser: flags.string({ + char: 'b', + description: messages.getMessage('browser'), + options: ['chrome', 'edge', 'firefox'], // These are ones supported by "open" package + exclusive: ['urlonly'], + }), clientid: flags.string({ char: 'i', description: commonMessages.getMessage('clientId'), @@ -102,7 +108,10 @@ export default class Login extends SfdxCommand { private async executeLoginFlow(oauthConfig: OAuth2Config): Promise { const oauthServer = await WebOAuthServer.create({ oauthConfig }); await oauthServer.start(); - await open(oauthServer.getAuthorizationUrl(), { wait: false }); + const openOptions = this.flags.browser + ? { app: { name: open.apps[this.flags.browser as string] as open.AppName }, wait: false } + : { wait: false }; + await open(oauthServer.getAuthorizationUrl(), openOptions); return oauthServer.authorizeAndSave(); } } From 52fdbf2b96c87b8294768e9152eb99bed2dc0206 Mon Sep 17 00:00:00 2001 From: Mounib Date: Wed, 30 Nov 2022 18:15:44 +0100 Subject: [PATCH 2/2] refactor: using flags.enum instead of flag.string --- messages/web.login.json | 2 +- src/commands/auth/web/login.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/messages/web.login.json b/messages/web.login.json index f765b91d..3d55f7cb 100644 --- a/messages/web.login.json +++ b/messages/web.login.json @@ -4,7 +4,7 @@ "$ sfdx auth:web:login -a TestOrg1", "$ sfdx auth:web:login -i ", "$ sfdx auth:web:login -r https://MyDomainName--SandboxName.sandbox.my.salesforce.com", - "sfdx auth:web:login -a TestOrg1 -b firefox" + "$ sfdx auth:web:login -a TestOrg1 -b firefox" ], "browser": "browser where the org opens", "deviceWarning": "auth:web:login doesn't work when authorizing to a headless environment. Use auth:device:login instead.", diff --git a/src/commands/auth/web/login.ts b/src/commands/auth/web/login.ts index 5d689b4b..0ba97d85 100644 --- a/src/commands/auth/web/login.ts +++ b/src/commands/auth/web/login.ts @@ -26,11 +26,10 @@ export default class Login extends SfdxCommand { public static aliases = ['force:auth:web:login']; public static readonly flagsConfig: FlagsConfig = { - browser: flags.string({ + browser: flags.enum({ char: 'b', description: messages.getMessage('browser'), options: ['chrome', 'edge', 'firefox'], // These are ones supported by "open" package - exclusive: ['urlonly'], }), clientid: flags.string({ char: 'i',