diff --git a/.gitignore b/.gitignore index 0023a53..d811c90 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ DerivedData/ .swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc +.env \ No newline at end of file diff --git a/Package.swift b/Package.swift index 822c4ec..174b64e 100644 --- a/Package.swift +++ b/Package.swift @@ -20,13 +20,13 @@ let package = Package( ), ], dependencies: [ - .package(url: "https://github.com/apple/swift-log.git", from: "1.6.0"), + .package(url: "https://github.com/apple/swift-log.git", from: "1.6.0") ], targets: [ .target( name: "RxAuthSwift", dependencies: [ - .product(name: "Logging", package: "swift-log"), + .product(name: "Logging", package: "swift-log") ] ), .target( diff --git a/Sources/RxAuthSwift/AuthenticationState.swift b/Sources/RxAuthSwift/AuthenticationState.swift index 3f90a81..9922332 100644 --- a/Sources/RxAuthSwift/AuthenticationState.swift +++ b/Sources/RxAuthSwift/AuthenticationState.swift @@ -18,4 +18,44 @@ public struct User: Codable, Identifiable, Sendable, Equatable { self.email = email self.image = image } + + enum CodingKeys: String, CodingKey { + case id + case sub + case name + case email + case image + case picture + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + // Support both "id" and "sub" (OIDC standard) for user identifier + if let id = try container.decodeIfPresent(String.self, forKey: .id) { + self.id = id + } else if let sub = try container.decodeIfPresent(String.self, forKey: .sub) { + self.id = sub + } else { + throw DecodingError.keyNotFound( + CodingKeys.id, + DecodingError.Context( + codingPath: container.codingPath, + debugDescription: "Neither 'id' nor 'sub' found in user info response" + ) + ) + } + self.name = try container.decodeIfPresent(String.self, forKey: .name) + self.email = try container.decodeIfPresent(String.self, forKey: .email) + // Support both "image" and "picture" (OIDC standard) for profile image + self.image = try container.decodeIfPresent(String.self, forKey: .image) + ?? container.decodeIfPresent(String.self, forKey: .picture) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encodeIfPresent(name, forKey: .name) + try container.encodeIfPresent(email, forKey: .email) + try container.encodeIfPresent(image, forKey: .image) + } }