Skip to content

Commit

Permalink
Update custom attribute handling to be more specific where we expect …
Browse files Browse the repository at this point in the history
…the values in the response body
  • Loading branch information
shilgapira committed Aug 13, 2024
1 parent 9fae2e7 commit 7f0b184
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/internal/http/DescopeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ class DescopeClient: HTTPClient {
var firstSeen: Bool

mutating func setValues(from data: Data, response: HTTPURLResponse) throws {
// extract JWTs from the cookies if configured to not return them in the response body
guard let url = response.url, let fields = response.allHeaderFields as? [String: String] else { return }
let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields, for: url)
for cookie in cookies where !cookie.value.isEmpty {
Expand All @@ -421,7 +422,12 @@ class DescopeClient: HTTPClient {
refreshJwt = cookie.value
}
}
try user?.setValues(from: data, response: response)

// the UserResponse decoding takes care of all fields except customAttributes
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] ?? [:]
if let dict = json["user"] as? [String: Any] {
user?.setCustomAttributes(from: dict)
}
}
}

Expand Down Expand Up @@ -451,14 +457,12 @@ class DescopeClient: HTTPClient {

mutating func setValues(from data: Data, response: HTTPURLResponse) throws {
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] ?? [:]
let user = json["user"] as? [String: Any] ?? [:]
if let customAttributes = json["customAttributes"] as? [String: Any] {
self.customAttributes = customAttributes
} else if let userCustomAttributes = user["customAttributes"] as? [String: Any] {
self.customAttributes = userCustomAttributes
} else {
self.customAttributes = [:]
}
setCustomAttributes(from: json)
}

mutating func setCustomAttributes(from dict: [String: Any]) {
guard let attrs = dict["customAttributes"] as? [String: Any] else { return }
customAttributes = attrs
}
}

Expand Down

0 comments on commit 7f0b184

Please sign in to comment.