Repository navigation
fix(auth): make the logged-in account the one the next command uses - #72
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
tg auth loginandtg auth login-qrdidn't make the account they logged in to the one the next command uses.activeAccountinconfig.json(the issue): every start makes that account active again, and a login only changeddata.db, so the command after a login went back to the configured account.is_activeset, 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
refactor: theactiveAccountwrite oftg accounts switch(tg accounts switch is undone by activeAccount in config.json #61) moves intosrc/utils/active-account.ts(keepActiveAccountInConfig). It writes only when the key is set and names another account, never createsconfig.json, and fails withINVALID_ARGSorGENERAL_ERROR(details.path). The switch behaves as before.fix(auth): a login now changes the active account only once it succeeds, and then makes its account the only active one indata.db. A failed login leaves the active account alone.fix(auth): after a successful login,AuthDependencies.keepActiveAccountInConfig(a new hook) writes the account's ID toactiveAccountby the same rule. It runs beforedata.dbchanges, as in the switch. The login has already happened at Telegram by then, so ifconfig.jsoncan't be written the command fails withGENERAL_ERROR. The message starts withLogged in as account <id>,detailsholdspathandaccount_id, the suggestion says to make the file writable or removeactiveAccountfrom it, then runtg 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 indata.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.mdanddocs/testing.mdnow describe the new behaviour.How I checked it
New tests in
src/__tests__/auth.test.ts("the account a login makes active"). They run bothloginWithPhoneandloginWithQrthrough the real default hook fromgetDefaultDependencies(), on a temp data dir of their own, and replay the next command's start withsyncActiveAccountFromConfig:config.json: the new account is the only active one, and no file is createdactiveAccount: 1: the file becomesactiveAccount: 2, other keys keptconfig.jsonwithout the key stays byte for byteconfig.jsonconfig.json:GENERAL_ERRORwithpath,account_idand the suggestion; the file and the active account unchangedWithout part 3, the
activeAccount: 1, read-only and merge cases fail (5 tests). Without part 2, the "noconfig.json" and failed-login cases fail (4 tests). The existing switch e2e tests inmulti-account.e2e.test.tscover the refactor.bun run check: cleanbun run test: 1722 passbun run test:e2e: 252 passNo 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: …) unlessAGENTS.mdsays otherwise🤖 Generated with Claude Code