diff --git a/ios-base/Common/Models/Session.swift b/ios-base/Common/Models/Session.swift index 27c5e64..467863c 100644 --- a/ios-base/Common/Models/Session.swift +++ b/ios-base/Common/Models/Session.swift @@ -15,7 +15,9 @@ struct Session: Codable { var accessToken: String? var expiry: Date? - var isValid: Bool { [uid, accessToken, client].allSatisfy { $0 != nil } } + var isValid: Bool { + [uid, accessToken, client].allSatisfy { !($0 ?? "").isEmpty } + } private enum CodingKeys: String, CodingKey { case uid @@ -34,19 +36,15 @@ struct Session: Codable { self.expiry = expires } - init?(headers: [AnyHashable: Any]) { - guard var stringHeaders = headers as? [String: String] else { - return nil - } - - stringHeaders.lowercaseKeys() - - if let expiryString = stringHeaders[HTTPHeader.expiry.rawValue], + init?(headers: [String: String]) { + var loweredKeysHeaders = headers + loweredKeysHeaders.lowercaseKeys() + if let expiryString = loweredKeysHeaders[APIClient.HTTPHeader.expiry.rawValue], let expiryNumber = Double(expiryString) { expiry = Date(timeIntervalSince1970: expiryNumber) } - uid = stringHeaders[HTTPHeader.uid.rawValue] - client = stringHeaders[HTTPHeader.client.rawValue] - accessToken = stringHeaders[HTTPHeader.token.rawValue] + uid = loweredKeysHeaders[APIClient.HTTPHeader.uid.rawValue] + client = loweredKeysHeaders[APIClient.HTTPHeader.client.rawValue] + accessToken = loweredKeysHeaders[APIClient.HTTPHeader.token.rawValue] } } diff --git a/ios-base/Networking/Services/AuthenticationServices.swift b/ios-base/Networking/Services/AuthenticationServices.swift index 82e075a..1771021 100644 --- a/ios-base/Networking/Services/AuthenticationServices.swift +++ b/ios-base/Networking/Services/AuthenticationServices.swift @@ -167,10 +167,12 @@ internal class AuthenticationServices { private func saveUserSession( _ user: User?, headers: [AnyHashable: Any] - ) -> Bool { - UserDataManager.currentUser = user - sessionManager.currentSession = Session(headers: headers) - - return UserDataManager.currentUser != nil && sessionManager.isSessionValid + ) { + UserDataManager.currentUser = User( + dictionary: response["user"] as? [String: Any] ?? [:] + ) + if let headers = headers as? [String: String] { + SessionManager.currentSession = Session(headers: headers) + } } }