# Draft upstream issue for gitroomhq/postiz-app (NOT FILED)

**Title:** TikTok: refresh token rotation will break channels on 2026-12-01 (fixed 23 h expiry, refresh token lost on profile-fetch failure, no refresh lock)

**Body**

TikTok announced that from 2026-12-01 access tokens last 2 hours, refresh tokens last 180 days, and every refresh returns a new refresh token and invalidates the old one at once. Reusing an old refresh token fails the refresh. Postiz v1.47.0 and current main (v2.25.3, checked 2026-11-02) have four problems on that path in `tiktok.provider.ts` and the refresh service:

1. `refreshToken()` and `authenticate()` set `expiresIn` to a fixed 23 hours instead of using TikTok's `expires_in`, so the stored `tokenExpiration` is wrong for a 2 hour token. The refresh then happens only after a 401.
2. `TiktokProvider` has no `refreshCron`, so `startRefreshWorkflow()` never schedules a timed refresh for it, and `refreshTokenWorkflow` sleeps until the exact expiry with no lead time.
3. In `refreshToken()`, after the token call rotates the refresh token, a second call to `/v2/user/info/` destructures `data.user` with no guard. If that call fails (5xx, timeout, rate limit), the new refresh token is dropped, `refreshProcess()` swallows the throw, flags `refreshNeeded` and disconnects the channel. The old token is already dead, so the channel cannot recover without a manual reconnect.
4. `RefreshIntegrationService.refresh()` has no lock and the post workflow passes a stale integration snapshot, so two refreshes (a timed one and a post, or two posts) can use the same old refresh token. TikTok rejects the second and the channel is disconnected, even though the first succeeded.

**What we did (Glass fork on v1.47.0):** use `expires_in`; set `refreshCron`; refresh 15 minutes before expiry; make the profile fetch best effort; take a per-channel Postgres advisory lock around the refresh, re-read the stored row after the lock, skip the call if another process already refreshed, and save the rotated tokens in the lock transaction last, after a best-effort full update. We tested this against a mock TikTok server that enforces the one-use rule, with a forced short expiry: 5 automatic early refreshes with 0 rejected reuses, a mid-run `/user/info` 503, a failing picture upload, 4 refreshes racing, and a stale-snapshot caller. Report and patch available on request.

Happy to open a PR against main (TypeScript, no new dependencies; the test hooks in our fork would be removed).
