Fix Swift 6 actor isolation in MarksAuth — use UserDefaults UUID
All checks were successful
CI / build-and-deploy (push) Successful in 22s

UIDevice.current is MainActor-isolated; replaced with a stable UUID
persisted in UserDefaults to avoid the async access.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Krishna Kumar
2026-05-22 14:20:40 -05:00
parent 57d5288b54
commit d2ff1d2f2f

View File

@@ -1,8 +1,7 @@
import Foundation
import UIKit
private let backendBaseURL = "https://chatai-realtime-proxy-production.up.railway.app"
private let deviceIdKey = "marksDeviceId"
private let tokenKey = "marksJWT"
private let tokenExpiryKey = "marksJWTExpiry"
@@ -15,6 +14,13 @@ enum MarksAuth {
return try await register()
}
private static func deviceId() -> String {
if let existing = UserDefaults.standard.string(forKey: deviceIdKey) { return existing }
let new = UUID().uuidString
UserDefaults.standard.set(new, forKey: deviceIdKey)
return new
}
private static func cached() -> String? {
guard
let token = UserDefaults.standard.string(forKey: tokenKey),
@@ -28,22 +34,19 @@ enum MarksAuth {
guard let url = URL(string: "\(backendBaseURL)/api/auth/register") else {
throw URLError(.badURL)
}
let deviceId = UIDevice.current.identifierForVendor?.uuidString ?? UUID().uuidString
let id = deviceId()
var req = URLRequest(url: url)
req.httpMethod = "POST"
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
req.httpBody = try JSONSerialization.data(withJSONObject: ["device_id": deviceId])
req.httpBody = try JSONSerialization.data(withJSONObject: ["device_id": id])
let (data, response) = try await URLSession.shared.data(for: req)
let status = (response as? HTTPURLResponse)?.statusCode ?? 0
guard (200...299).contains(status) else {
throw APIError.badStatus(status)
}
guard (200...299).contains(status) else { throw APIError.badStatus(status) }
struct Resp: Decodable { let access_token: String; let expires_in: Int }
let resp = try JSONDecoder().decode(Resp.self, from: data)
UserDefaults.standard.set(resp.access_token, forKey: tokenKey)
let expiry = Date().addingTimeInterval(TimeInterval(resp.expires_in))
UserDefaults.standard.set(expiry, forKey: tokenExpiryKey)
print("[Auth] Registered device \(deviceId.prefix(8))")
UserDefaults.standard.set(Date().addingTimeInterval(TimeInterval(resp.expires_in)), forKey: tokenExpiryKey)
print("[Auth] Registered \(id.prefix(8))")
return resp.access_token
}
}