Skip to content

Commit

Permalink
feat: support targeting browser in login command (#537)
Browse files Browse the repository at this point in the history
* feat: support targeting browser in login command

* refactor: using flags.enum instead of flag.string
  • Loading branch information
aemounib authored Nov 30, 2022
1 parent 2aed4be commit 6eb35e0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion command-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"noprompt",
"setalias",
"setdefaultdevhubusername",
"setdefaultusername"
"setdefaultusername",
"browser"
],
"alias": ["force:auth:web:login"]
}
Expand Down
6 changes: 4 additions & 2 deletions messages/web.login.json
Original file line number Diff line number Diff line change
@@ -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 <OAuth client id>",
"$ 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"
}
10 changes: 9 additions & 1 deletion src/commands/auth/web/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -102,7 +107,10 @@ export default class Login extends SfdxCommand {
private async executeLoginFlow(oauthConfig: OAuth2Config): Promise<AuthInfo> {
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();
}
}
Expand Down

0 comments on commit 6eb35e0

Please sign in to comment.