diff --git a/ios-base/Common/Models/Session.swift b/ios-base/Common/Models/Session.swift index f4f0bb94..467863c9 100644 --- a/ios-base/Common/Models/Session.swift +++ b/ios-base/Common/Models/Session.swift @@ -15,6 +15,10 @@ struct Session: Codable { var accessToken: String? var expiry: Date? + var isValid: Bool { + [uid, accessToken, client].allSatisfy { !($0 ?? "").isEmpty } + } + private enum CodingKeys: String, CodingKey { case uid case client @@ -32,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/Managers/SessionManager.swift b/ios-base/Managers/SessionManager.swift index 4c554886..cb6923f3 100644 --- a/ios-base/Managers/SessionManager.swift +++ b/ios-base/Managers/SessionManager.swift @@ -39,11 +39,7 @@ internal class SessionManager: CurrentUserSessionProvider { userDefaults.removeObject(forKey: "ios-base-session") } - var validSession: Bool { - if let session = currentSession, let uid = session.uid, - let tkn = session.accessToken, let client = session.client { - return !uid.isEmpty && !tkn.isEmpty && !client.isEmpty - } - return false + static var isSessionValid: Bool { + currentSession?.isValid ?? false } } diff --git a/ios-base/Navigators/AppNavigator.swift b/ios-base/Navigators/AppNavigator.swift index dfe91a24..690f0c6b 100644 --- a/ios-base/Navigators/AppNavigator.swift +++ b/ios-base/Navigators/AppNavigator.swift @@ -10,7 +10,7 @@ import Foundation internal class AppNavigator: BaseNavigator { - static let shared = AppNavigator(isLoggedIn: SessionManager.shared.validSession) + static let shared = AppNavigator(isLoggedIn: SessionManager.shared.isSessionValid) init(isLoggedIn: Bool) { let initialRoute: Route = isLoggedIn diff --git a/ios-base/Networking/Services/AuthenticationServices.swift b/ios-base/Networking/Services/AuthenticationServices.swift index 4fb4885b..17710213 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.validSession + ) { + UserDataManager.currentUser = User( + dictionary: response["user"] as? [String: Any] ?? [:] + ) + if let headers = headers as? [String: String] { + SessionManager.currentSession = Session(headers: headers) + } } } diff --git a/ios-baseUnitTests/Services/UserServiceUnitTests.swift b/ios-baseUnitTests/Services/UserServiceUnitTests.swift index 0991c288..739e1e1c 100644 --- a/ios-baseUnitTests/Services/UserServiceUnitTests.swift +++ b/ios-baseUnitTests/Services/UserServiceUnitTests.swift @@ -77,7 +77,7 @@ class UserServiceUnitTests: XCTestCase { headers: unusableHeaders ) XCTAssert(SessionManager.currentSession == nil) - XCTAssertFalse(SessionManager.validSession) + XCTAssertFalse(SessionManager.isSessionValid) // Testing case where should be session but not valid let wrongSessionHeaders = [ @@ -91,6 +91,6 @@ class UserServiceUnitTests: XCTestCase { headers: wrongSessionHeaders ) XCTAssert(SessionManager.currentSession != nil) - XCTAssertFalse(SessionManager.validSession) + XCTAssertFalse(SessionManager.isSessionValid) } }