-
Notifications
You must be signed in to change notification settings - Fork 44
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
base: main
Are you sure you want to change the base?
Fix uploadData calling fetchCredentials when token has not expired #528
Conversation
|
Hey @Omnicpie, I think that while this PR does work - it's possible that we might want to approach it slightly differently. You are welcome to update this PR to match the Dart implemention, but it might be a bit of work though. Otherwise we do have this work in our backlog and will get to this in the near future. |
hey @Chriztiaan, if I'm understanding those dart implementations correctly, does that mean that we always assume that credentials are OK if they are present (in dart land), and only in the case that we have 401'd and wiped the creds, then we generate new ones on the next request? if that is the case, what 401 would that be? Is it only the 401 from the sync service itself, or would an error from the uploadData function also trigger that functionality? |
Yes, we depend on the 401 to determine whether the credentials are invalid or not.
From our side we are concerned about the powersync stream 401s. Users would normally manage their own session invalidation for uploads - but it could be the same, just that we don't enforce it. One possible challenge is detecting this status code for websockets. I haven't looked into this as of yet. |
Currently, as part of the flow to build a request for uploadData, we call
getCredentials()
within AbstractRemote. This getCredentials function does not check / use the currently stored set of credentials unless there is a time in which the developer providesexpiresAt
as part of theirfetchCredentials()
response, Since this is not included in the typing, its relatively unlikely that this would be the case, so for most users we will be making excess calls to fetchCredentials when the current set of stored creds is still valid.This PR expands the
getCredentials()
request to get the current token stored, decode the JWT data to get the currentexp
field (if available), and perform the same check that would happen for a manually providedexpiresAt
field.Testing
I tested this using the
example-vite
demo with some minor alterations, along with a self-hosted powersync instance.Below is a code snippet of the main changes to the example, mostly just giving a valid token and endpoint to the return of
fetchCredentials
. There was also an addition of a button to trigger an insert.Before changes
Before the change showed the issue well, when we called uploadData (via pressing the button to insert an action), we could see that it would log
insert
for theuploadData
method, but also that it would logfetch
for thefetchCredentials
method:After changes
Post changes, when calling the endpoint with a valid token, we could see that
insert
was still logged, but thefetch
log was no longer present, due toexp
on the token still being valid.