Skip to content

Commit 04ce093

Browse files
committed
fix(realtime): ensure custom JWT token is set before channel subscriptions
1 parent 4d6b6f6 commit 04ce093

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

packages/core/realtime-js/src/RealtimeClient.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ export default class RealtimeClient {
186186
}
187187

188188
this._setConnectionState('connecting')
189-
this._setAuthSafely('connect')
190189

191190
// Establish WebSocket connection
192191
if (this.transport) {
@@ -253,11 +252,13 @@ export default class RealtimeClient {
253252
this._setConnectionState('disconnected')
254253
}
255254

256-
// Close the WebSocket connection
257-
if (code) {
258-
this.conn.close(code, reason ?? '')
259-
} else {
260-
this.conn.close()
255+
// Close the WebSocket connection if close method exists
256+
if (typeof this.conn.close === 'function') {
257+
if (code) {
258+
this.conn.close(code, reason ?? '')
259+
} else {
260+
this.conn.close()
261+
}
261262
}
262263

263264
this._teardownConnection()
@@ -608,7 +609,11 @@ export default class RealtimeClient {
608609

609610
// Wait for any pending auth operations before flushing send buffer
610611
// This ensures channel join messages include the correct access token
611-
this._waitForAuthIfNeeded()
612+
const authPromise =
613+
this._authPromise ||
614+
(this.accessToken && !this.accessTokenValue ? this.setAuth() : Promise.resolve())
615+
616+
authPromise
612617
.then(() => {
613618
this.flushSendBuffer()
614619
})

packages/core/supabase-js/test/integration.test.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -363,31 +363,45 @@ describe('Storage API', () => {
363363
describe('Custom JWT', () => {
364364
describe('Realtime', () => {
365365
test('will connect with a properly signed jwt token', async () => {
366-
const jwtToken = sign({ sub: '1234567890' }, JWT_SECRET, { expiresIn: '1h' })
366+
const jwtToken = sign(
367+
{
368+
sub: '1234567890',
369+
role: 'anon',
370+
iss: 'supabase-demo',
371+
},
372+
JWT_SECRET,
373+
{ expiresIn: '1h' }
374+
)
367375
const supabaseWithCustomJwt = createClient(SUPABASE_URL, ANON_KEY, {
368376
accessToken: () => Promise.resolve(jwtToken),
377+
realtime: {
378+
...(wsTransport && { transport: wsTransport }),
379+
},
369380
})
370381

371-
// Wait for subscription using Promise to avoid polling
372-
await new Promise<void>((resolve, reject) => {
373-
const timeout = setTimeout(() => {
374-
reject(new Error('Timeout waiting for subscription'))
375-
}, 10000)
376-
377-
supabaseWithCustomJwt.channel('test-channel').subscribe((status, err) => {
378-
if (status === 'SUBSCRIBED') {
379-
clearTimeout(timeout)
380-
// Verify token was set
381-
expect(supabaseWithCustomJwt.realtime.accessTokenValue).toBe(jwtToken)
382-
resolve()
383-
} else if (status === 'CHANNEL_ERROR' || status === 'TIMED_OUT') {
384-
clearTimeout(timeout)
385-
reject(err || new Error(`Subscription failed with status: ${status}`))
386-
}
382+
try {
383+
// Wait for subscription using Promise to avoid polling
384+
await new Promise<void>((resolve, reject) => {
385+
const timeout = setTimeout(() => {
386+
reject(new Error('Timeout waiting for subscription'))
387+
}, 4000)
388+
389+
supabaseWithCustomJwt.channel('test-channel').subscribe((status, err) => {
390+
if (status === 'SUBSCRIBED') {
391+
clearTimeout(timeout)
392+
// Verify token was set
393+
expect(supabaseWithCustomJwt.realtime.accessTokenValue).toBe(jwtToken)
394+
resolve()
395+
} else if (status === 'CHANNEL_ERROR' || status === 'TIMED_OUT') {
396+
clearTimeout(timeout)
397+
reject(err || new Error(`Subscription failed with status: ${status}`))
398+
}
399+
})
387400
})
388-
})
389-
390-
await supabaseWithCustomJwt.removeAllChannels()
391-
}, 15000)
401+
} finally {
402+
// Always cleanup channels and connection, even if test fails
403+
await supabaseWithCustomJwt.removeAllChannels()
404+
}
405+
}, 5000)
392406
})
393407
})

0 commit comments

Comments
 (0)