Skip to content

Supabase.initialize don't refresh token after 1h on app open again #906

Closed

Description

@gabrielviannadev

Describe the bug
From 1 week and a couple days ago, we have a problem in our application, basically, before this time, users use login with OTP normally, and have a refresh token automatically, every good.

But now, after this days, when user login with OTP, this generate a valid token, but, if they close and access app again after 1 hour, this is logged out, because the token is no more valid.

The problem is, i use Supase from 1 year, and never have this problem, i use Supabase.initialize all time on start app, and this check if the old token is valid, if not, refresh that. But now, don't work anymore.

Debbuging, i see the client.auth.session <- its comming null on app after this 1hour time.

To Reproduce
Only starts Supabase.initialiize, this generate a token in console -> put app closed in background, and re-open after a medium time, this retorn the same token (don't refresh).

Expected behavior
The token needs to be refresh when i open app again, when the old token its not valid, and goTrue Session not be null.

Screenshots

Version (please complete the following information):
On Linux/macOS
I'm using latest version of Flutter, and latest version of Supabase supabase_flutter 2.5.1.

On Windows

Additional context
I think explained all infos.

Activity

  1. added
    bug
    on Apr 26, 2024
  2. changed the title [-]Supabase.initialiize don't refresh token after 1h on app open again[/-] [+]Supabase.initialize don't refresh token after 1h on app open again[/+] on Apr 26, 2024
  3. added
    auth
    on Apr 26, 2024
  4. dshukertjr commented on Apr 26, 2024

    @dshukertjr
    Member

    @gabrielviannadev I cannot reproduce this on my end, but are you able to reproduce it? Do you have reliable steps to reproduce every time, or do you see this happening only once in a while?

  5. com8member2 commented on May 1, 2024

    @com8member2

    i am also facing the issue but i is not happening consistently but many times when app is opened after some time is gives below error and user gets sign out.

    invalid JWT: unable to parse or verify signature, token is expired by 2m5s
    I/flutter (18692): #0      GotrueFetch.request (package:gotrue/src/fetch.dart:99:7)
    I/flutter (18692): <asynchronous suspension>
    I/flutter (18692): #1      GoTrueAdminApi.signOut (package:gotrue/src/gotrue_admin_api.dart:42:5)
    I/flutter (18692): <asynchronous suspension>
    I/flutter (18692): #2      GoTrueClient.signOut (package:gotrue/src/gotrue_client.dart:813:9)
    I/flutter (18692): <asynchronous suspension>
    I/flutter (18692): #3      GoTrueClient._callRefreshToken (package:gotrue/src/gotrue_client.dart:1115:11)
    I/flutter (18692): <asynchronous suspension>
    I/flutter (18692): #4      GoTrueClient.recoverSession (package:gotrue/src/gotrue_client.dart:928:16)
    I/flutter (18692): <asynchronous suspension>
    I/flutter (18692): #5      SupabaseAuth.recoverSession (package:supabase_flutter/src/supabase_auth.dart:86:11)
    I/flutter (18692): <asynchronous suspension>
    I/flutter (18692): #6      CancelableCompleter.complete.<anonymous closure> (package:async/src/cancelable_operation.dart:425:16)
    I/flutter (18692): <asynchronous suspension>
    
  6. marektomczyk commented on May 24, 2024

    @marektomczyk

    I experience the same problem on my production app - users are randomly logged out from the app. In Supabase logs, I see exactly the same log as @com8member2 included above. Please bump priority on it!

  7. dshukertjr commented on May 28, 2024

    @dshukertjr
    Member

    @com8member2 @marektomczyk
    So this random logout happens not when the user launches the app but when they have the app opened for a long time?

  8. tomekit commented on Jun 7, 2024

    @tomekit

    In our case when app is closed for more than an hour, token expires which is expected. Problem happens when app is started again, we then issue the API request using the most recent JWT (which has expired) which results in API call failing with 403 Forbidden.
    The: _callRefreshToken logic auto refreshes the token successfully, but new token is received shortly (depending on network conditions etc.) after the initial request was issued.

    Perhaps: _refreshTokenCompleter could be exposed, so we could either await on it if it's Future or even run: _callRefreshToken ourselves to ensure that latest JWT is fetched before any API call is made.

  9. Vinzent03 commented on Jun 15, 2024

    @Vinzent03
    Collaborator

    @tomekit What's your use case to manually use the latest jwt? If you are calling supabase endpoints via the sdk, the sdk should try to get a new jwt before making the actual request.

  10. zhangheng2022 commented on Jul 1, 2024

    @zhangheng2022

    Maybe we need an example

  11. tomekit commented on Jul 1, 2024

    @tomekit

    Hi @Vinzent03,

    Sorry for late reply. In our case we're using Supabase mostly for auth and then pass JWT to external APIs outside of Supabase ecosystem.

    I think it's fair to assume that there are many systems (legacy or not) which would like to integrate authentication mechanism and not necessarily would like to be rewritten using Supabase mindset.
    Given that JWT integration is extremely easy I think it's rather common use case to "connect" other systems to unified auth system which Supabase's GoTrue is (https://github.com/supabase/gotrue).
    The current downside is that Supbase doesn't support public keys, so currently sharing secret to external API shares private key, which isn't inherently secure: https://github.com/orgs/supabase/discussions/12759#discussioncomment-9584184)
    It would be also good to allow optional 2FA JWT verification, we've added that ourselves: supabase/auth@8878f32 but that's probably bit offtopic.

    My point is, that if Supabase Flutter allows user to access current session:

    Session? get currentSession => _currentSession;

    it already leaks enough internals (and that's good, because it allows JWT external use) that it should probably allow user to access Future to await to make sure that currentSession is up to date, but that's my opinion only.

  12. Vinzent03 commented on Jul 2, 2024

    @Vinzent03
    Collaborator

    Calling refreshSeession() if the session is expired (session.isExpired) should fix it. It returns the future to the already ongoing request or starts a new refresh.

  13. tomekit commented on Jul 2, 2024

    @tomekit

    Thanks for you reply. I've just double checked our codebase and that's actually what we've been doing already... and in fact it should behave as you describe.
    Sorry for the confusion caused, we're still getting some intermittent issues with not up to date token, but it seems that this issue likely lies somewhere on our end, as Flutter's goauth library resolved most of these over the past months.

  14. dshukertjr commented on Sep 4, 2024

    @dshukertjr
    Member

    @vikrvm Do you have a way to reproduce this issue consistently? That would help us figure out what the root cause is.

  15. dshukertjr commented on Sep 4, 2024

    @dshukertjr
    Member

    @vikrvm Is it a Swift or Flutter application running on iOS?

  16. 7 remaining items

  17. dshukertjr commented on Dec 15, 2024

    @dshukertjr
    Member

    @zcmgyu

    Your access token is being refreshed, but you are trying to connect to Realtime before you receive the new accessToken, which is causing this error is what I think is happening here. This should certainly be handled by the client, so we will confirm on our end if this is really happening, but make necessary changes.

    For now, you can just make sure to only connect to realtime after you receive the tokenRefreshed auth event.

  18. zcmgyu commented on Dec 16, 2024

    @zcmgyu

    @dshukertjr Thank you. Just to confirm, I’ve already addressed this case across all real-time channels, and I hope it resolves the issue.

    Future<void> _handleSubscribeLobby(
        RealtimeSubscribeStatus status, Object? error) async {
      if ([
        RealtimeSubscribeStatus.channelError,
        RealtimeSubscribeStatus.timedOut,
      ].contains(status)) {
        logger.i('Subscribe lobby status: $status');
        logger.i('Subscribe lobby error: $error');
        if (error.toString().contains('Invalid value for JWT claim')) {
          logger.i('Session is expired while warm up lobby, refreshing session');
          await supabase.auth.refreshSession();
        }
        await _restartLobbySubscription();
      }
    }
  19. dshukertjr commented on Dec 16, 2024

    @dshukertjr
    Member

    @zcmgyu Not sure if that will fix the issue, but did it work?

    Once this version is released, it should fix the issue for you.

  20. zcmgyu commented on Dec 17, 2024

    @zcmgyu

    @dshukertjr I'm not sure it worked. Until now, that issue did not occur. Waiting for it :D

  21. tomekit commented on Dec 29, 2024

    @tomekit

    In our case token wasn't automatically refreshing in the background after upgrading to 2.8.2 release.
    We've downgraded to 2.8.0 and things are back to normal.
    This is applicable to web version of our app: https://web.s3drive.app/

    Desktop and mobile seem to be unaffected and refreshing takes places normally on 2.8.2.

  22. dimeloper commented on May 26, 2025

    @dimeloper

    Are there any updates here? We're experiencing the same issue. I suppose we should also downgrade to version 2.8.0?

  23. Vinzent03 commented on May 28, 2025

    @Vinzent03
    Collaborator

    I ran a minimal flutter web app for over an hour and had no issues. Could it be the case you are affected by the following issue: #1164 So basically you are using the new shared preference apis, which are incompatible with us still using the old and soon deprecated apis?

  24. github-actions commented on Mar 1, 2026

    @github-actions
    Contributor

    This issue has been inactive for 180 days.
    It will be automatically closed in 30 days if there is no further activity.

    If you believe this issue is still relevant, please leave a comment to keep it open.
    Thank you for your contributions!

  25. 0xPixelfrost commented on Mar 22, 2026

    @0xPixelfrost

    Any updates on this? My user still get logged out over night.

    If I had known beforehand that this would become such a long and complex issue, I wouldn't have chosen Supabase. This is an essential feature.

  26. added 2 commits that reference this issue on Jun 10, 2026
    de2adf9
    368609a
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

    authbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions