supabase / auth-js

An isomorphic Javascript library for Supabase Auth.
MIT License
357 stars 160 forks source link

fix: always wait for `_initialize` before loading the session #747

Closed hf closed 1 year ago

hf commented 1 year ago

Previously __loadSession didn't wait for initialization to finish if stack guards were not supported which broke some flows.

This fix attempts to identify recursive calls to _initialize, i.e. recursive waiting for inititalizePromise:

  1. When the session is detected from URL, GoTrueClient needs to call getUser(accessTokenFromURL) to fetch the user. But this then depended on _useSession, which calls __loadSession which waits for initializePromise. With this PR, it no longer calls _useSession if the access token is provided.
  2. _onVisibilityChanged could be called from within #_initialize which would block. Now it's called with a setTimeout of 0 so the processing takes place in the next event loop pass.
  3. _startAutoRefresh which calls _onAutoRefreshTokenTick also could be called from within #_initialize and the same setTimeout with 0 trick is applied.

This should get rid of all issues with a loading spinner / endless waits when fetching the session from the URL or with password recovery.

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 2.46.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: