Skip to content

Commit c74ce06

Browse files
committed
fix(realtime): remove jwt check
Also: - Call `setAuth` after heartbeat - Send `x-client-info` when joining channel
1 parent dea204f commit c74ce06

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

Sources/Realtime/RealtimeChannelV2.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ public final class RealtimeChannelV2: Sendable {
104104

105105
let payload = RealtimeJoinPayload(
106106
config: joinConfig,
107-
accessToken: await socket._getAccessToken()
107+
accessToken: await socket._getAccessToken(),
108+
version: socket.options.headers[.xClientInfo]
108109
)
109110

110111
let joinRef = socket.makeRef()

Sources/Realtime/RealtimeClientV2.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,18 @@ public final class RealtimeClientV2: Sendable {
117117
wsTransport: @escaping WebSocketTransport,
118118
http: any HTTPClientType
119119
) {
120+
var options = options
121+
if options.headers[.xClientInfo] == nil {
122+
options.headers[.xClientInfo] = "realtime-swift/\(version)"
123+
}
124+
120125
self.url = url
121126
self.options = options
122127
self.wsTransport = wsTransport
123128
self.http = http
124129
apikey = options.apikey
125130

126-
mutableState.withValue {
131+
mutableState.withValue { [options] in
127132
if let accessToken = options.headers[.authorization]?.split(separator: " ").last {
128133
$0.accessToken = String(accessToken)
129134
} else {
@@ -353,15 +358,15 @@ public final class RealtimeClientV2: Sendable {
353358
if Task.isCancelled {
354359
break
355360
}
356-
self?.sendHeartbeat()
361+
await self?.sendHeartbeat()
357362
}
358363
}
359364
mutableState.withValue {
360365
$0.heartbeatTask = heartbeatTask
361366
}
362367
}
363368

364-
private func sendHeartbeat() {
369+
private func sendHeartbeat() async {
365370
let pendingHeartbeatRef: String? = mutableState.withValue {
366371
if $0.pendingHeartbeatRef != nil {
367372
$0.pendingHeartbeatRef = nil
@@ -383,6 +388,7 @@ public final class RealtimeClientV2: Sendable {
383388
payload: [:]
384389
)
385390
)
391+
await setAuth()
386392
} else {
387393
options.logger?.debug("Heartbeat timeout")
388394
reconnect()
@@ -426,14 +432,6 @@ public final class RealtimeClientV2: Sendable {
426432
token = mutableState.accessToken
427433
}
428434

429-
if let token, let payload = JWT.decodePayload(token),
430-
let exp = payload["exp"] as? TimeInterval, exp < Date().timeIntervalSince1970
431-
{
432-
options.logger?.warning(
433-
"InvalidJWTToken: Invalid value for JWT claim \"exp\" with value \(exp)")
434-
return
435-
}
436-
437435
mutableState.withValue { [token] in
438436
$0.accessToken = token
439437
}

Sources/Realtime/RealtimeJoinConfig.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import Foundation
1010
struct RealtimeJoinPayload: Codable {
1111
var config: RealtimeJoinConfig
1212
var accessToken: String?
13+
var version: String?
1314

1415
enum CodingKeys: String, CodingKey {
1516
case config
1617
case accessToken = "access_token"
18+
case version
1719
}
1820
}
1921

0 commit comments

Comments
 (0)