From 6b1ca165f9658d222e0fcded4674183c6c6a0c75 Mon Sep 17 00:00:00 2001 From: Franco Leto Date: Tue, 22 Feb 2022 19:12:43 -0300 Subject: [PATCH 1/2] Let a session decide if is valid or not --- ios-base/Common/Models/Session.swift | 2 ++ ios-base/Managers/SessionManager.swift | 8 ++------ ios-base/Navigators/AppNavigator.swift | 2 +- ios-base/Networking/Services/AuthenticationServices.swift | 2 +- ios-baseUnitTests/Services/UserServiceUnitTests.swift | 4 ++-- 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/ios-base/Common/Models/Session.swift b/ios-base/Common/Models/Session.swift index f4f0bb94..27c5e642 100644 --- a/ios-base/Common/Models/Session.swift +++ b/ios-base/Common/Models/Session.swift @@ -15,6 +15,8 @@ struct Session: Codable { var accessToken: String? var expiry: Date? + var isValid: Bool { [uid, accessToken, client].allSatisfy { $0 != nil } } + private enum CodingKeys: String, CodingKey { case uid case client 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..82e075af 100644 --- a/ios-base/Networking/Services/AuthenticationServices.swift +++ b/ios-base/Networking/Services/AuthenticationServices.swift @@ -171,6 +171,6 @@ internal class AuthenticationServices { UserDataManager.currentUser = user sessionManager.currentSession = Session(headers: headers) - return UserDataManager.currentUser != nil && sessionManager.validSession + return UserDataManager.currentUser != nil && sessionManager.isSessionValid } } 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) } } From e49f9105ced2775938ae4eed2d7c6b55fc828f5d Mon Sep 17 00:00:00 2001 From: Franco Leto Date: Tue, 22 Feb 2022 19:22:13 -0300 Subject: [PATCH 2/2] Changing Type in Session Init just to avoid inside conversion --- ios-base/Common/Models/Session.swift | 22 +++++++++---------- .../Services/AuthenticationServices.swift | 12 +++++----- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/ios-base/Common/Models/Session.swift b/ios-base/Common/Models/Session.swift index 27c5e642..467863c9 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 82e075af..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.isSessionValid + ) { + UserDataManager.currentUser = User( + dictionary: response["user"] as? [String: Any] ?? [:] + ) + if let headers = headers as? [String: String] { + SessionManager.currentSession = Session(headers: headers) + } } }