Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Revert "fix(studio): delegate check for expired token to gotrue" (#41268)

Charis16cdd025b7bc
diff --git a/packages/common/auth.tsx b/packages/common/auth.tsx
index ee8a1dacd1d..661c67daac1 100644
--- a/packages/common/auth.tsx
+++ b/packages/common/auth.tsx
@@ -159,21 +159,27 @@ gotrueClient.onAuthStateChange((event, session) => {
})
/**
- * Gets a current access token.
- *
- * Calls getSession, which will refresh the token if needed.
+ * Grabs the currently available access token, or calls getSession.
*/
export async function getAccessToken() {
// ignore if server-side
if (typeof window === 'undefined') return undefined
- const {
- data: { session },
- error,
- } = await gotrueClient.getSession()
- if (error) {
- throw error
+ const aboutToExpire = currentSession?.expires_at
+ ? currentSession.expires_at - Math.ceil(Date.now() / 1000) < 30
+ : false
+
+ if (!currentSession || aboutToExpire) {
+ const {
+ data: { session },
+ error,
+ } = await gotrueClient.getSession()
+ if (error) {
+ throw error
+ }
+
+ return session?.access_token
}
- return session?.access_token
+ return currentSession.access_token
}