realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.81k stars 578 forks source link

Realm Web: Refresh access tokens ahead of time #3967

Open kraenhansen opened 3 years ago

kraenhansen commented 3 years ago

Problem

Users are experiencing messages in the browsers console as a request fails due to an expired access token. Even though these errors doesn't inflict any functional pain on developers or their users, we they're a slight inconvenience and we should eventually find a way to get rid of them.

Solution

Two solutions comes to mind:

  1. Check the expiration of the access token and compare it to the browsers time and ensure to preflight a request to refresh it if it's expired.
  2. Refresh the access token on a timeout, to ensure it is refreshed ahead of time, before a request is made.

I would suggest we go for option 1, since 2 is not guaranteed to actually refresh before a request (ex. the computer was hibernating as the timeout should fire). Also option 2 would probably cause warnings in React Native (as scheduling a timeout in the far future does).

How important is this improvement for you?

Fairly niche, but nice to have anyway.

ironage commented 3 years ago

Do you know what version this was reported on? We should have implemented the first solution that you mention above, in the object store layer since core 10.6.1. However, if it is not working in recent versions, then I'd like to investigate why, perhaps something is going wrong with how we are getting the current time in a browser environment.

nirinchev commented 3 years ago

@ironage judging by the issue title, this is happening in realm-web which is a js-only implementation that doesn't use Core/OS. It offers the remote mongo API, auth, functions, and so on, but obviously no persistence.

ironage commented 3 years ago

@nirinchev good call, looks like you're correct about that. Glad that something isn't broken with the fix in object-store 😄