Skip to content

Commit 9a5b18a

Browse files
Ry AndersonRy Anderson
authored andcommitted
Assume stored credentials are always valid when present
1 parent c5f6845 commit 9a5b18a

File tree

2 files changed

+3
-22
lines changed

2 files changed

+3
-22
lines changed

packages/common/src/client/sync/stream/AbstractRemote.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ export type RemoteConnector = {
2020
const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
2121
const POWERSYNC_JS_VERSION = PACKAGE.version;
2222

23-
// Refresh at least 30 sec before it expires
24-
const REFRESH_CREDENTIALS_SAFETY_PERIOD_MS = 30_000;
2523
const SYNC_QUEUE_REQUEST_LOW_WATER = 5;
2624

2725
// Keep alive message is sent every period
@@ -122,21 +120,11 @@ export abstract class AbstractRemote {
122120
}
123121

124122
async getCredentials(): Promise<PowerSyncCredentials | null> {
125-
const { expiresAt } = this.credentials ?? {};
126-
const currentTime = new Date(new Date().valueOf() + REFRESH_CREDENTIALS_SAFETY_PERIOD_MS);
127-
if (expiresAt && expiresAt > currentTime) {
123+
// If we have credentials stored, return them.
124+
if (this.credentials) {
128125
return this.credentials!;
129126
}
130127

131-
// Check if existing stored token is still valid
132-
if (this.credentials?.token) {
133-
const { exp } = this.parseToken(this.credentials?.token) ?? {};
134-
135-
if (exp && new Date(Number(exp) * 1000) > currentTime) {
136-
return this.credentials!;
137-
}
138-
}
139-
140128
this.credentials = await this.connector.fetchCredentials();
141129
if (this.credentials?.endpoint.match(POWERSYNC_TRAILING_SLASH_MATCH)) {
142130
throw new Error(
@@ -150,14 +138,6 @@ export abstract class AbstractRemote {
150138
return `powersync-js/${POWERSYNC_JS_VERSION}`;
151139
}
152140

153-
private parseToken(token: string) {
154-
try {
155-
return JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
156-
} catch (_) {
157-
return null;
158-
}
159-
}
160-
161141
protected async buildRequest(path: string) {
162142
const credentials = await this.getCredentials();
163143
if (credentials != null && (credentials.endpoint == null || credentials.endpoint == '')) {

packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ The next upload iteration will be delayed.`);
296296
break;
297297
}
298298
} catch (ex) {
299+
// TODO: Handle 401s to invalidate cached credentials
299300
checkedCrudItem = undefined;
300301
this.updateSyncStatus({
301302
dataFlow: {

0 commit comments

Comments
 (0)