Repository navigation
fix: re-initialise poll loop after re-authentication (#175) - #176
Merged
Merged
Conversation
The running poll loop is built once at startup from the stored OAuth token. handleOAuthCallback only persisted the new token to the auth state and the in-memory JSON, but never rebuilt the pollRunner / token provider, so a re-authentication had no effect until the instance was manually restarted — the loop kept using the stale (often expired) token. Add restartPolling() and call it after persistTokenJson in the OAuth callback so the token provider is rebuilt from the new token. Stopping the previous loop first prevents two concurrent poll loops. Adds a regression test that drives the OAuth start-link + callback path with a stubbed fetch and asserts the device is re-bound with the new token after re-auth (fails with only the pre-fix single bind). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Fixes #175.
Problem
After re-authenticating via the admin OAuth flow, the adapter kept using the old in-memory token:
info.connectionstayedfalseand no telemetry was written until the instance was manually restarted (iobroker restart bluetti.0).Root cause:
handleOAuthCallback()completes the login and callspersistTokenJson(), which only updates the in-memorythis.oauthTokenJsonstring and the encryptedauth.tokenJsonstate. The liveBluettiPollRunner+BluettiStoredTokenProviderare built once instartPolling()atonReadyand capture the token at that time — nothing rebuilds them, and there is nosubscribeStateson the token. So the running poll loop never sees the new token.This bit a real instance whose refresh token had expired (
invalid_grant): re-auth in admin appeared to do nothing, and only a manual restart recovered it.Fix
restartPolling(): stop the currentpollRunnerand callstartPolling()again, rebuilding the token provider from the updatedthis.oauthTokenJson.persistTokenJson()inhandleOAuthCallback().Stopping the previous loop first prevents two concurrent poll loops.
startPolling()stays a graceful no-op when auth/device are not yet configured (first-time login before device selection), and changing the device still restarts the instance via native-config as before.Test
Adds a regression test that drives the OAuth start-link + callback path with a stubbed
fetchand asserts the device is re-bound with the new token (TOKEN_B) after re-auth. Without the fix only the initial bind (TOKEN_A) happens and the assertion fails.npx tsc --noEmitandeslintare clean.