Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth session doesn't refresh when using database #77

Closed
ryansburgoyne opened this issue Apr 5, 2023 · 13 comments · Fixed by #395
Closed

Auth session doesn't refresh when using database #77

ryansburgoyne opened this issue Apr 5, 2023 · 13 comments · Fixed by #395
Assignees
Labels
bug Something isn't working

Comments

@ryansburgoyne
Copy link

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 ryansburgoyne added the bug Something isn't working label Apr 5, 2023
@grdsdev
Copy link
Collaborator

grdsdev commented Jun 8, 2023

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.

@kurtguenther
Copy link

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
Copy link

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

@grdsdev
Copy link
Collaborator

grdsdev commented Aug 1, 2023

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 grdsdev closed this as completed Aug 1, 2023
@bstillitano
Copy link

bstillitano commented Apr 5, 2024

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

@grdsdev
Copy link
Collaborator

grdsdev commented Apr 5, 2024

Hi @bstillitano shouldn't need to manually refresh.

I'll reopen this issue to investigate, thanks.

@grdsdev grdsdev reopened this Apr 5, 2024
@bstillitano
Copy link

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

@florian-mlr
Copy link

Same issue here, is there any news ?

@grdsdev
Copy link
Collaborator

grdsdev commented Apr 21, 2024

@bstillitano and @florian-mlr

Are you using PostgresClient directly, or through the SupabaseClient?

@bstillitano
Copy link

bstillitano commented Apr 21, 2024

@bstillitano and @florian-mlr

Are you using PostgresClient directly, or through the SupabaseClient?

@grdsdev Supabase client only

@junaama
Copy link

junaama commented May 8, 2024

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

@JOsacky
Copy link

JOsacky commented May 17, 2024

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

It looks like #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 :)

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