Skip to content

Commit d3026be

Browse files
authored
fix: call setAuth on subscribe (#499)
1 parent 1981e97 commit d3026be

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/RealtimeClient.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ export default class RealtimeClient {
205205
* Connects the socket, unless already connected.
206206
*/
207207
connect(): void {
208+
// Set the token on connect
209+
setTimeout(() => {
210+
this.setAuth().catch((e) => {
211+
this.log('error', 'error setting auth', e)
212+
})
213+
}, 0)
208214
if (this.conn) {
209215
return
210216
}
@@ -216,12 +222,6 @@ export default class RealtimeClient {
216222
}
217223
this.conn = new this.transport(this.endpointURL()) as WebSocketLike
218224
this.setupConnection()
219-
// Set the token on connect
220-
setTimeout(() => {
221-
this.setAuth().catch((e) => {
222-
this.log('error', 'error setting auth', e)
223-
})
224-
}, 0)
225225
}
226226

227227
/**

test/RealtimeChannel.test.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'assert'
22
import sinon from 'sinon'
3-
import crypto from 'crypto'
3+
import crypto, { randomUUID } from 'crypto'
44
import { describe, beforeEach, afterEach, test, vi } from 'vitest'
55

66
import RealtimeClient from '../src/RealtimeClient'
@@ -169,7 +169,7 @@ describe('constructor', () => {
169169

170170
describe('subscribe', () => {
171171
beforeEach(() => {
172-
channel = socket.channel('topic', { one: 'two' })
172+
channel = socket.channel('topic')
173173
})
174174

175175
afterEach(() => {
@@ -208,6 +208,7 @@ describe('subscribe', () => {
208208

209209
assert.equal(channel.state, CHANNEL_STATES.joining)
210210
})
211+
211212
test('updates join push payload access token', () => {
212213
socket.accessTokenValue = 'token123'
213214

@@ -221,10 +222,30 @@ describe('subscribe', () => {
221222
postgres_changes: [],
222223
private: false,
223224
},
224-
one: 'two',
225225
})
226226
})
227227

228+
test('triggers setAuth when socket is not connected', async () => {
229+
clock.restore() // Use real timers for this test
230+
let callCount = 0
231+
const tokens = [randomUUID(), randomUUID()]
232+
const accessToken = async () => tokens[callCount++]
233+
const testSocket = new RealtimeClient(url, {
234+
accessToken: accessToken,
235+
transport: WebSocket,
236+
})
237+
const channel = testSocket.channel('topic')
238+
239+
channel.subscribe()
240+
await new Promise((resolve) => setTimeout(resolve, 0))
241+
assert.equal(channel.socket.accessTokenValue, tokens[0])
242+
243+
testSocket.disconnect()
244+
channel.subscribe()
245+
await new Promise((resolve) => setTimeout(resolve, 0))
246+
assert.equal(channel.socket.accessTokenValue, tokens[1])
247+
})
248+
228249
test('triggers socket push with default channel params', () => {
229250
sinon.stub(socket, '_makeRef').callsFake(() => defaultRef)
230251
const spy = sinon.spy(socket, 'push')
@@ -247,7 +268,6 @@ describe('subscribe', () => {
247268
postgres_changes: [],
248269
private: false,
249270
},
250-
one: 'two',
251271
},
252272
ref: defaultRef,
253273
join_ref: defaultRef,
@@ -325,7 +345,6 @@ describe('subscribe', () => {
325345
],
326346
private: false,
327347
},
328-
one: 'two',
329348
},
330349
ref: defaultRef,
331350
join_ref: defaultRef,

0 commit comments

Comments
 (0)