diff --git a/packages/common/src/client/sync/stream/AbstractRemote.ts b/packages/common/src/client/sync/stream/AbstractRemote.ts index d0b6ab61..094c94c0 100644 --- a/packages/common/src/client/sync/stream/AbstractRemote.ts +++ b/packages/common/src/client/sync/stream/AbstractRemote.ts @@ -20,8 +20,6 @@ export type RemoteConnector = { const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/; const POWERSYNC_JS_VERSION = PACKAGE.version; -// Refresh at least 30 sec before it expires -const REFRESH_CREDENTIALS_SAFETY_PERIOD_MS = 30_000; const SYNC_QUEUE_REQUEST_LOW_WATER = 5; // Keep alive message is sent every period @@ -122,10 +120,11 @@ export abstract class AbstractRemote { } async getCredentials(): Promise { - const { expiresAt } = this.credentials ?? {}; - if (expiresAt && expiresAt > new Date(new Date().valueOf() + REFRESH_CREDENTIALS_SAFETY_PERIOD_MS)) { + // If we have credentials stored, return them. + if (this.credentials) { return this.credentials!; } + this.credentials = await this.connector.fetchCredentials(); if (this.credentials?.endpoint.match(POWERSYNC_TRAILING_SLASH_MATCH)) { throw new Error( diff --git a/packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts b/packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts index 6522497f..bc4c7116 100644 --- a/packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +++ b/packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts @@ -296,6 +296,7 @@ The next upload iteration will be delayed.`); break; } } catch (ex) { + // TODO: Handle 401s to invalidate cached credentials checkedCrudItem = undefined; this.updateSyncStatus({ dataFlow: {