From 6eb35e0d23d9988665f7b85a5bead428ed654c7c Mon Sep 17 00:00:00 2001 From: Mounib Date: Wed, 30 Nov 2022 23:15:03 +0100 Subject: [PATCH] feat: support targeting browser in login command (#537) * feat: support targeting browser in login command * refactor: using flags.enum instead of flag.string --- command-snapshot.json | 3 ++- messages/web.login.json | 6 ++++-- src/commands/auth/web/login.ts | 10 +++++++++- 3 files changed, 15 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..3d55f7cb 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..0ba97d85 100644 --- a/src/commands/auth/web/login.ts +++ b/src/commands/auth/web/login.ts @@ -26,6 +26,11 @@ export default class Login extends SfdxCommand { public static aliases = ['force:auth:web:login']; public static readonly flagsConfig: FlagsConfig = { + browser: flags.enum({ + char: 'b', + description: messages.getMessage('browser'), + options: ['chrome', 'edge', 'firefox'], // These are ones supported by "open" package + }), clientid: flags.string({ char: 'i', description: commonMessages.getMessage('clientId'), @@ -102,7 +107,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(); } }