supabase-swift icon indicating copy to clipboard operation
supabase-swift copied to clipboard

Auth session doesn't refresh when using database

Open ryansburgoyne opened this issue 1 year ago • 12 comments

Bug report

Describe the bug

If my JWT has expired since I last called supabaseClient.auth.session, and I try to execute a command using supabaseClient.database, I get an error about the JWT being expired. I have to work around this by calling _ = supabaseClient.auth.session before every database call to force it to refresh the token.

To Reproduce

  1. Log in using Supabase Auth
  2. Wait for the token to expire
  3. Try to perform an operation on a database

Expected behavior

I would expect the session to automatically be refreshed before attempting the database operation.

System information

  • OS: macOS
  • Version of supabase-swift: 0.2.1

ryansburgoyne avatar Apr 05 '23 23:04 ryansburgoyne

At some point it was there: https://github.com/supabase-community/supabase-swift/commit/098cc9390064ac499c1ae3a2e216cc1b3e7b8a09#diff-8ae3298b041e73e9ea309814eeb66aeb229f08265773c84cad2a9e83349ff5caR93

but then it was removed? https://github.com/supabase-community/supabase-swift/commit/13293398a1d1fe74268c4db37f745c46977262f1#diff-8ae3298b041e73e9ea309814eeb66aeb229f08265773c84cad2a9e83349ff5caL114

Pedr0Rocha avatar Apr 27 '23 21:04 Pedr0Rocha

When you call auth.session it checks for a valid session and automatically refreshes it if needed, so the removal of the auth.refreshCurrentSessionIfNeeded() method call should not be an issue.

I'll investigate it better.

grdsdev avatar Jun 08 '23 20:06 grdsdev

I too am running into this issue (I get 401 when I restart my app after the JWT expires). I agree with @grsouza that (at least from my read of the GoTrue code) the token should already automatically refresh when auth.session is fetched.

https://github.com/supabase-community/gotrue-swift/blob/4bf132d73d322ee5d6d648f02deb9f2bc19cc659/Sources/GoTrue/Internal/SessionManager.swift#L41C18-L41C18

  func session() async throws -> Session {
    if let task = task {
      return try await task.value
    }

    guard let currentSession = try Env.localStorage.getSession() else {
      throw GoTrueError.sessionNotFound
    }

    if currentSession.isValid {
      return currentSession.session
    }

/////////// REFRESHING HERE ///////////
    task = Task {
      defer { self.task = nil }

      let session = try await Env.sessionRefresher(currentSession.session.refreshToken)
      try update(session)
      return session
    }

    return try await task!.value
  }

kurtguenther avatar Aug 01 '23 21:08 kurtguenther

After some investigation, this is what was causing the erroneous 401 for me (I had kept testing the JWT during the minute where this would have been incorrectly marked as isValid when it was already expired).

So it should be fixed as of PR 55 for GoTrue

kurtguenther avatar Aug 01 '23 22:08 kurtguenther

Closing this issue as it seems to be solved, if it happens again, we can re-open it.

Thank you all for the investigation.

grdsdev avatar Aug 01 '23 22:08 grdsdev

I'm getting this issue still when making requests using Postgres. Are we meant to manually update the token on the clients?

Simulator Screenshot - iPhone 15 Pro - 2024-04-05 at 23 51 07 Simulator Screenshot - iPhone 15 Pro - 2024-04-05 at 23 52 11

bstillitano avatar Apr 05 '24 12:04 bstillitano

Hi @bstillitano shouldn't need to manually refresh.

I'll reopen this issue to investigate, thanks.

grdsdev avatar Apr 05 '24 13:04 grdsdev

No probs @grdsdev let me know if I can help in any way.

bstillitano avatar Apr 05 '24 13:04 bstillitano

Same issue here, is there any news ?

florian-mlr avatar Apr 21 '24 07:04 florian-mlr

@bstillitano and @florian-mlr

Are you using PostgresClient directly, or through the SupabaseClient?

grdsdev avatar Apr 21 '24 10:04 grdsdev

@bstillitano and @florian-mlr

Are you using PostgresClient directly, or through the SupabaseClient?

@grdsdev Supabase client only

bstillitano avatar Apr 21 '24 10:04 bstillitano

Also experiencing the same issue, is there an update on the way?

junaama avatar May 08 '24 16:05 junaama

Also experiencing the same issue, is there an update on the way?

It looks like https://github.com/supabase/supabase-swift/pull/353 will fix the issue but we're waiting on review approval

@grdsdev if there is anything we can do to help with this PR, let us know :)

JOsacky avatar May 17 '24 18:05 JOsacky