The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(cli): default unnamed login connections to default (#27412)
Weiko636de8af378d
diff --git a/packages/twenty-cli/README.md b/packages/twenty-cli/README.mdindex 6ec4f73dbe7..fa20b6e610b 100644--- a/packages/twenty-cli/README.md+++ b/packages/twenty-cli/README.md@@ -21,7 +21,7 @@ The examples below use `twenty` for the built executable. Run it as```bashtwenty doctor --offline-twenty auth login --url https://acme.twenty.com --name dev --use+twenty auth login --url https://acme.twenty.comtwenty metadata object listtwenty data list companies --limit 5twenty opendiff --git a/packages/twenty-cli/docs/commands.md b/packages/twenty-cli/docs/commands.mdindex 0c0a10d79b3..ba0c6243062 100644--- a/packages/twenty-cli/docs/commands.md+++ b/packages/twenty-cli/docs/commands.md@@ -21,6 +21,15 @@ failure recovery. See the [README](../README.md) to build and start the CLI.## Connections and authentication+```bash+twenty auth login --url https://acme.twenty.com+twenty auth status+```++The connection is named `default` when neither `--name` nor `--remote` is supplied.+Run `twenty auth login` again to sign in to that saved URL. To keep several+connections, give each a name:+```bashtwenty auth login --url https://acme.twenty.com --name dev --usetwenty auth status --remote dev@@ -42,6 +51,8 @@ printf '%s' "$TWENTY_API_KEY" | twenty auth login --with-token --url https://acmCredentials and remote settings are saved in `~/.twenty/config.json`, withowner-only file permissions. Login validates credentials before saving them.`--use` makes the remote the default; the first remote also becomes the default.+Creating the connection named `default` does not switch an existing default+remote unless you pass `--use`.Use `twenty auth login --remote dev` to sign in again to a saved URL. Changing anexisting remote's URL requires `--replace`.diff --git a/packages/twenty-cli/src/commands/auth/__tests__/auth-and-remote-commands.spec.ts b/packages/twenty-cli/src/commands/auth/__tests__/auth-and-remote-commands.spec.tsindex 38378d92815..74efebb271a 100644--- a/packages/twenty-cli/src/commands/auth/__tests__/auth-and-remote-commands.spec.ts+++ b/packages/twenty-cli/src/commands/auth/__tests__/auth-and-remote-commands.spec.ts@@ -103,6 +103,57 @@ describe('auth and remote commands', () => {});});+ it('saves an unnamed connection as default and reuses its URL on the next login', async () => {+ const first = await login(['--url', server.url]);+ const again = await login([]);++ expect(first.exitCode).toBe(0);+ expect(first.envelope.data).toMatchObject({+ remote: 'default',+ isDefault: true,+ });+ expect(again.exitCode).toBe(0);+ expect(again.envelope.data).toEqual(first.envelope.data);+ expect(await readConfigFile()).toMatchObject({+ defaultRemote: 'default',+ remotes: { default: { apiUrl: server.url, apiKey: VALID_KEY } },+ });+ });++ it('asks for a URL when no connections have been saved', async () => {+ const { envelope, exitCode } = await runJson(['auth', 'login']);++ expect(exitCode).toBe(2);+ expect(envelope.error).toMatchObject({+ code: 'USAGE',+ message: 'No saved connections yet.',+ hint: 'Run twenty auth login --url <url> to save a connection.',+ });+ await expect(readFile(configPath)).rejects.toMatchObject({+ code: 'ENOENT',+ });+ });++ it('keeps a selected named remote and protects the unnamed connection from URL replacement', async () => {+ await login(['--url', server.url, '--name', 'prod']);+ const unnamed = await login(['--url', server.url]);+ const refused = await login(['--url', secondServerUrl]);++ expect(unnamed.envelope.data).toMatchObject({+ remote: 'default',+ isDefault: false,+ });+ expect(refused.exitCode).toBe(2);+ expect(refused.envelope.error.code).toBe('CONFIRMATION_REQUIRED');+ expect(await readConfigFile()).toMatchObject({+ defaultRemote: 'prod',+ remotes: {+ prod: { apiUrl: server.url },+ default: { apiUrl: server.url },+ },+ });+ });+it('saves nothing when the server rejects the key', async () => {const { envelope, exitCode } = await login(['--url', server.url, '--name', 'prod'],diff --git a/packages/twenty-cli/src/commands/auth/login/auth-login.command-definition.ts b/packages/twenty-cli/src/commands/auth/login/auth-login.command-definition.tsindex a8217712f8b..4932589a642 100644--- a/packages/twenty-cli/src/commands/auth/login/auth-login.command-definition.ts+++ b/packages/twenty-cli/src/commands/auth/login/auth-login.command-definition.ts@@ -12,7 +12,10 @@ export const AUTH_LOGIN_COMMAND_DEFINITION: LocalCommandDefinition = {flags: '--url <url>',description: 'Twenty server URL, for example https://acme.twenty.com',},- { flags: '--name <name>', description: 'Name of the remote to save' },+ {+ flags: '--name <name>',+ description: 'Name of the remote to save (defaults to "default")',+ },{ flags: '--use', description: 'Make this remote the default' },{flags: '--replace',@@ -20,6 +23,7 @@ export const AUTH_LOGIN_COMMAND_DEFINITION: LocalCommandDefinition = {},],examples: [+ 'twenty auth login --url https://acme.twenty.com','twenty auth login --url https://acme.twenty.com --name prod','printf \'%s\' "$TWENTY_API_KEY" | twenty auth login --with-token --url https://acme.twenty.com --name prod','twenty auth login --with-token --remote prod < api-key.txt',diff --git a/packages/twenty-cli/src/commands/auth/login/run-auth-login-command.ts b/packages/twenty-cli/src/commands/auth/login/run-auth-login-command.tsindex 0eff4cdd83a..47b6fff2609 100644--- a/packages/twenty-cli/src/commands/auth/login/run-auth-login-command.ts+++ b/packages/twenty-cli/src/commands/auth/login/run-auth-login-command.ts@@ -59,22 +59,6 @@ const assertSameUrlOrReplace = ({});};-const readRemoteName = (options: Record<string, unknown>) => {- const remoteName =- readStringOption(options, 'name') ?? readStringOption(options, 'remote');-- if (!isDefined(remoteName)) {- throw new CliError({- code: 'USAGE',- exitCode: EXIT_CODE.USAGE,- message: 'Name the remote with --name.',- hint: 'To sign in to a saved remote again, pass --remote <name>.',- });- }-- return validateRemoteName(remoteName);-};-const obtainCredentials = async ({options,apiUrl,@@ -127,8 +111,8 @@ const formatSignedIn = ({: '';return credentials.kind === 'apiKey'- ? `Saved remote ${remoteName} ${dimText(`(${apiUrl}) · API key${workspace}`)}`- : `Signed in to ${remoteName}${isDefined(identity.email) ? ` as ${identity.email}` : ''}${dimText(workspace)}`;+ ? `Saved remote "${remoteName}" ${dimText(`(${apiUrl}) · API key${workspace}`)}`+ : `Signed in to "${remoteName}"${isDefined(identity.email) ? ` as ${identity.email}` : ''}${dimText(workspace)}`;};export const runAuthLoginCommand: CommandRun = async ({@@ -137,21 +121,26 @@ export const runAuthLoginCommand: CommandRun = async ({outputMode,signal,}) => {- const remoteName = readRemoteName(options);+ const remoteNameOption =+ readStringOption(options, 'name') ?? readStringOption(options, 'remote');+ const remoteName = validateRemoteName(remoteNameOption ?? 'default');const replace = readBooleanOption(options, 'replace');const configPath = getConfigPath();- const existingRemote = findExistingRemote(- await readConfig(configPath),- remoteName,- );+ const config = await readConfig(configPath);+ const existingRemote = findExistingRemote(config, remoteName);const urlOption = readStringOption(options, 'url');if (!isDefined(urlOption) && !isDefined(existingRemote)) {throw new CliError({code: 'USAGE',exitCode: EXIT_CODE.USAGE,- message: `There is no remote named ${remoteName} yet.`,- hint: 'Pass --url to create it.',+ message:+ Object.keys(config.remotes).length === 0+ ? 'No saved connections yet.'+ : `There is no remote named "${remoteName}" yet.`,+ hint: !isDefined(remoteNameOption)+ ? 'Run twenty auth login --url <url> to save a connection.'+ : 'Pass --url to create it.',});}@@ -253,13 +242,8 @@ export const runAuthLoginCommand: CommandRun = async ({email: identity.email,isDefault,},- human: [- formatSuccessLine(- formatSignedIn({ remoteName, apiUrl, credentials, identity }),- ),- ...(isDefault- ? [dimText(` ${remoteName} is your default remote.`)]- : []),- ].join('\n'),+ human: formatSuccessLine(+ formatSignedIn({ remoteName, apiUrl, credentials, identity }),+ ),};};