Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Network error removes session data #141

Closed

Description

@dshukertjr

Bug report

Describe the bug

By looking at the code here, it seems like whenever user opens the app while offline, the session is deleted.

const { error } = await this._callRefreshToken(currentSession.refresh_token)
if (error) {
  console.log(error.message)
  await this._removeSession()
}

To Reproduce

  1. Create an app using Supabase
  2. Sign in with your favorite method
  3. Turn on air plane mode
  4. Open the app and your are signed out

Expected behavior

Session should not be deleted when offline, and refreshing session should be attempted upon device regaining internet connection.

Additional context

I guess this issue has two challenges that it needs to overcome:

  1. detect network error and in such case, do not remove session (I guess this is pretty easy)
  2. in case the error was a network error, detect when the device becomes online again and retry session recovery

Activity

  1. added
    bug
    on Sep 18, 2021
  2. dshukertjr commented on Sep 20, 2021

    @dshukertjr
    MemberAuthor

    I was thinking if removing session only if status code is present, and if not, retry session recovery here.

    if (error) {
      const status = error?.status
      console.log(error.message)
      if(status) {
        await this._removeSession()
      } else {
        // retry session recovery here
      }
    }

    I feel like this might work, but at the same time, I haven't really thought it though, so I think there are some edge cases where this might be a problem.

  3. changed the title [-]Opening the app while offline deletes user session. [/-] [+]Network error remove session data[/+] on Dec 30, 2022
  4. changed the title [-]Network error remove session data[/-] [+]Network error removes session data[/+] on Dec 30, 2022
  5. ferrannp commented on Mar 22, 2023

    @ferrannp

    This issue is quite old but this still happening in latest version of supabase-js v2. Basically, if you open your app with the Internet off, your session will be killed. This seems extremely critical.

    If somebody reads this and have the same problem, I ended up handling session persistence in my side.

  6. nahtnam commented on Apr 23, 2023

    @nahtnam

    I've been having this same issue as well! My temporary solution was to extend the session tokens out to be much longer.

    If the initialize fails the user is immediately logged out: https://github.com/supabase/gotrue-js/blob/eb96536a441d21fae75d25ab6de346808c6e6044/src/GoTrueClient.ts#L1166-L1173 (as you pointed out in the issue). Unfortunately, we don't have the ability to control the initialize function as it's immediately run on createClient. Even the autoRefreshToken prop won't help.

    On the other hand, the auto refresher has a nice exponential backoff logic: https://github.com/supabase/gotrue-js/blob/eb96536a441d21fae75d25ab6de346808c6e6044/src/GoTrueClient.ts#L1089

    Extending the session token time will reduce the chance the user has to refresh the token on initialize and instead will use the auto refresher which handles the issue more gracefully.


    It would be nice if we could have a flag like autoInitialize: false so that we can call supabaseClient.initialize() ourselves. If the initialize fails, then we as developers can decide what to do:

    1. Retry
    2. Show an error message telling the user to go online (and catch the exception in sentry.io/bugsnag to debug)
    3. Log the user out <- Current default
  7. GaryAustin1 commented on Apr 24, 2023

    @GaryAustin1

    I'll also do some testing on this. Missed this was the case.

    I've noticed being logged out more than I suspect to be in my PWA, which can run offline and expects to just be able to do API operations when the connection is reestablished without a new signIn. It uses indexedDB to store write and file data and then when connection is established sends the requests back to Supabase. I use realtime and the connection error "failed to fetch" to go offline mode and then expects to be still have a session (or have token refreshed) when it comes back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions