Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let a session decide if is valid or not #196

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions ios-base/Common/Models/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
}
}
8 changes: 2 additions & 6 deletions ios-base/Managers/SessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion ios-base/Navigators/AppNavigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions ios-base/Networking/Services/AuthenticationServices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
4 changes: 2 additions & 2 deletions ios-baseUnitTests/Services/UserServiceUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -91,6 +91,6 @@ class UserServiceUnitTests: XCTestCase {
headers: wrongSessionHeaders
)
XCTAssert(SessionManager.currentSession != nil)
XCTAssertFalse(SessionManager.validSession)
XCTAssertFalse(SessionManager.isSessionValid)
}
}