Skip to content

fix(auth): make the logged-in account the one the next command uses - #72

Merged
neoromantic merged 6 commits into
mainfrom
fix/login-active-account-62
Oct 22, 2026
Merged

neoromantic merged 6 commits into
mainfrom
fix/login-active-account-62

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

What and why

tg auth login and tg auth login-qr didn't make the account they logged in to the one the next command uses.

  • With activeAccount in config.json (the issue): every start makes that account active again, and a login only changed data.db, so the command after a login went back to the configured account.
  • Without the key (found while writing the tests): a new account was created with is_active set, but the account that was active before stayed active too. The active-account query takes the lowest ID, so the next command still used the old account. A re-login of an existing account also made it active before Telegram accepted the code, so a failed login switched the active account.

Fixes #62

Fix

  1. refactor: the activeAccount write of tg accounts switch (tg accounts switch is undone by activeAccount in config.json #61) moves into src/utils/active-account.ts (keepActiveAccountInConfig). It writes only when the key is set and names another account, never creates config.json, and fails with INVALID_ARGS or GENERAL_ERROR (details.path). The switch behaves as before.
  2. fix(auth): a login now changes the active account only once it succeeds, and then makes its account the only active one in data.db. A failed login leaves the active account alone.
  3. fix(auth): after a successful login, AuthDependencies.keepActiveAccountInConfig (a new hook) writes the account's ID to activeAccount by the same rule. It runs before data.db changes, as in the switch. The login has already happened at Telegram by then, so if config.json can't be written the command fails with GENERAL_ERROR. The message starts with Logged in as account <id>, details holds path and account_id, the suggestion says to make the file writable or remove activeAccount from it, then run tg accounts switch --id <id> (removing the key alone would leave the old account active), and the active account stays as it was in both files. The account record and session the login wrote stay in data.db.

The issue's Fix section covered only config.json. Part 2 goes beyond it because without it the "without the key" case still sends the next command to the old account, so a test for that case can't pass.

docs/configuration.md (Which account a command uses, Supported keys, Runtime Behavior), docs/auth.md and docs/testing.md now describe the new behaviour.

How I checked it

New tests in src/__tests__/auth.test.ts ("the account a login makes active"). They run both loginWithPhone and loginWithQr through the real default hook from getDefaultDependencies(), on a temp data dir of their own, and replay the next command's start with syncActiveAccountFromConfig:

Case Phone QR
No config.json: the new account is the only active one, and no file is created ✓ ✓
activeAccount: 1: the file becomes activeAccount: 2, other keys kept ✓ ✓
config.json without the key stays byte for byte ✓ ✓
A failed login changes neither the active account nor config.json ✓ ✓
Read-only config.json: GENERAL_ERROR with path, account_id and the suggestion; the file and the active account unchanged ✓ ✓
The key already names the account: the file stays as it is ✓
A failed re-login of an existing account leaves the active one alone ✓
A QR login that merges into an existing account writes that account's ID ✓

Without part 3, the activeAccount: 1, read-only and merge cases fail (5 tests). Without part 2, the "no config.json" and failed-login cases fail (4 tests). The existing switch e2e tests in multi-account.e2e.test.ts cover the refactor.

  • bun run check: clean

  • bun run test: 1722 pass

  • bun run test:e2e: 252 pass

  • No live Telegram run: the change doesn't touch any Telegram call.

  • Tests pass locally and use made-up data only

  • README, docs and affected demos follow the product content standard

  • Title follows the repo's commit style: Conventional Commits (feat: …, fix: …) unless AGENTS.md says otherwise

🤖 Generated with Claude Code

neoromantic and others added 6 commits October 22, 2026 15:46
tg auth login and login-qr need the same rule as tg accounts switch:
write the new account to activeAccount in config.json when the key is
set and names another account, never create the file, and fail with
INVALID_ARGS or GENERAL_ERROR (details.path) when it can't be read or
written. Move that block into one helper so both use the same code. The
switch behaves as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tg auth login and login-qr created a new account with is_active set but
left the account that was active before active too. The active-account
query takes the lowest ID, so the command after a login kept using the
old account even without activeAccount in config.json. A re-login of an
existing account also made it active before Telegram accepted the code,
so a failed login switched the active account.

A successful login now makes its account the only active one in data.db,
and a failed login leaves the active account as it was.

Part of #62

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…son sets it

Every command except tg config, tg skill and tg daemon makes the account
that activeAccount in config.json names active at startup. tg auth login
and login-qr only changed data.db, so with the key set the command after
a login used the configured account again, not the one just logged in.

A successful login now writes its account to activeAccount by the same
rule as tg accounts switch (#61), through the helper they share: only
when the key is set and names another account, and config.json is never
created. config.json is written before data.db, so when it can't be
written the login fails with GENERAL_ERROR, says which account it logged
in (message and details.account_id) and leaves the active account as it
was. AuthDependencies gets a keepActiveAccountInConfig hook so the unit
tests run both login paths, with and without the key, offline.

Fixes #62

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…te activeAccount

When config.json can't be written after a login, data.db keeps the old
active account. The suggestion offered "remove activeAccount from it" as
a fix of its own, but with the key gone the next start applies nothing
and the old account stays active. Both fixes now lead to
tg accounts switch, in the error and in docs/configuration.md, and the
test pins the suggestion.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
"A failed login changes neither file" was wrong for data.db: a failed
phone login with a new number keeps the account record it created before
calling Telegram. What the login guarantees, and what the tests check, is
that activeAccount and the active account in data.db stay as they were.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The phone merge test checked only the returned account, so passing the
deleted placeholder's ID to the activation went unnoticed: activeAccount
got a missing ID and no account in data.db stayed active. The QR merge
already had such a check; now the phone merge asserts the ID the
activeAccount hook gets and the active account in data.db.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit 62a2187 into main Oct 22, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/login-active-account-62 branch October 22, 2026 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tg auth login / login-qr don't update activeAccount in config.json, so the next command uses the configured account

1 participant