supabase-swift
supabase-swift copied to clipboard
Auth session doesn't refresh when using database
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
- Log in using Supabase Auth
- Wait for the token to expire
- 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
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
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.
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
}
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
Closing this issue as it seems to be solved, if it happens again, we can re-open it.
Thank you all for the investigation.
I'm getting this issue still when making requests using Postgres. Are we meant to manually update the token on the clients?
Hi @bstillitano shouldn't need to manually refresh.
I'll reopen this issue to investigate, thanks.
No probs @grdsdev let me know if I can help in any way.
Same issue here, is there any news ?
@bstillitano and @florian-mlr
Are you using PostgresClient directly, or through the SupabaseClient?
@bstillitano and @florian-mlr
Are you using PostgresClient directly, or through the SupabaseClient?
@grdsdev Supabase client only
Also experiencing the same issue, is there an update on the way?
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 :)