Skip to content

Infinite token refresh loop when the device clock is >= 1 h ahead #650

Open

Description

@Mw3y

Version

@nuxtjs/supabase: v2.0.5 (handler unchanged in v2.1.0)
nuxt: v4.5.2
@supabase/supabase-js / auth-js: v2.103.3

Reproduction Link

No repository. Self-contained Node script below: it uses @supabase/supabase-js, a mocked auth server with 20 ms latency, Date.now shifted forward, and the onAuthStateChange handler copied from runtime/plugins/supabase.client.js.

repro.mjs
import { createClient } from '@supabase/supabase-js'

const SKEW_S = Number(process.argv[2] ?? 0)
const realNow = Date.now.bind(Date)
Date.now = () => realNow() + SKEW_S * 1000 // device clock ahead of real time

const b64 = (o) => Buffer.from(JSON.stringify(o)).toString('base64url')
const user = { id: '00000000-0000-0000-0000-000000000001', aud: 'authenticated', role: 'authenticated' }
let refreshes = 0, n = 0
function session() { // issued on the server's (correct) clock
  const iat = Math.floor(realNow() / 1000), exp = iat + 3600
  const jwt = `${b64({ alg: 'HS256', typ: 'JWT' })}.${b64({ sub: user.id, role: 'authenticated', iat, exp })}.sig`
  return { access_token: jwt, token_type: 'bearer', expires_in: 3600, expires_at: exp, refresh_token: `rt${n++}`, user }
}
const json = (b) => new Response(JSON.stringify(b), { status: 200, headers: { 'content-type': 'application/json' } })
const fetch = async (url) => {
  await new Promise(r => setTimeout(r, 20))
  const u = String(url)
  if (u.includes('grant_type=refresh_token')) { refreshes++; return json(session()) }
  if (u.endsWith('/auth/v1/user')) return json(user)
  return json({})
}
const store = new Map()
const client = createClient('http://fake.local', 'anon', {
  global: { fetch },
  auth: { autoRefreshToken: false, persistSession: true,
    storage: { getItem: k => store.get(k) ?? null, setItem: (k, v) => store.set(k, v), removeItem: k => store.delete(k) } },
})
await client.auth.setSession({ access_token: session().access_token, refresh_token: 'rt-initial' })
refreshes = 0

// Handler from runtime/plugins/supabase.client.js
let currentSession = null
client.auth.onAuthStateChange((_, s) => {
  if (JSON.stringify(currentSession) !== JSON.stringify(s)) {
    currentSession = s
    if (s?.user) client.auth.getClaims().then(() => {}, () => {})
  }
})
process.on('unhandledRejection', () => {})
await client.auth.getClaims().catch(() => {}) // one page:start
await new Promise(r => setTimeout(r, 3000))
console.log(`clock ${SKEW_S / 60} min ahead: ${refreshes} refreshes in 3 s`)
process.exit(0)

Steps to reproduce

  1. npm i @supabase/supabase-js and save the script above as repro.mjs.
  2. Run node repro.mjs 0, node repro.mjs 3480 and node repro.mjs 3600 (clock ahead by 0 min, 58 min and 60 min).

In a real app: set the computer's clock one hour or more ahead of real time (e.g. local time stored as UTC on a dual-boot machine), sign in, and watch the network tab.

What is Expected?

A token refresh happens roughly once per JWT lifetime, whatever the device clock says.

What is actually happening?

When the device clock is at least about 58.5 min ahead of real time (default 1 h JWT), auth-js treats every newly issued token as expired. The plugin's onAuthStateChange handler calls getClaims() on every session change. getClaims() calls getSession(), which refreshes the token, which emits TOKEN_REFRESHED, which calls the handler again. The result is an endless refresh loop that the user never sees.

Repro output: 0 refreshes in 3 s at 0 and +58 min, 142 at +60 min, 144 at +120 min. In production, one such user produced about 30k refreshes per hour (388k refresh tokens in a week) while their tab was open.

Suggested fix: only fetch claims when the user changes. page:start already refreshes claims on navigation. With this change the repro stops after 2 refreshes.

client.auth.onAuthStateChange((_, session: Session | null) => {
if (JSON.stringify(currentSession.value) !== JSON.stringify(session)) {
currentSession.value = session
if (session?.user) {
client.auth.getClaims().then(({ data }) => {
currentUser.value = data?.claims ?? null
})
}
else {
currentUser.value = null
}
}
})

 if (session?.user) {
-  client.auth.getClaims().then(({ data }) => {
-    currentUser.value = data?.claims ?? null;
-  });
+  if (session.user.id !== currentUser.value?.sub) {
+    client.auth.getClaims().then(({ data }) => {
+      currentUser.value = data?.claims ?? null;
+    });
+  }
 } else {

Co-Authored-By: Claude Opus 5.5 [email protected]

Activity

  1. added
    bug
    on Oct 21, 2026
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions