Skip to content

Fix uploadData calling fetchCredentials when token has not expired #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/common/src/client/sync/stream/AbstractRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -122,10 +120,11 @@ export abstract class AbstractRemote {
}

async getCredentials(): Promise<PowerSyncCredentials | null> {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down