Skip to content

Commit

Permalink
Let a session decide if is valid or not
Browse files Browse the repository at this point in the history
  • Loading branch information
LetoFranco committed Jan 27, 2023
1 parent 6311f2e commit 6b1ca16
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 10 deletions.
2 changes: 2 additions & 0 deletions ios-base/Common/Models/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
2 changes: 1 addition & 1 deletion ios-base/Networking/Services/AuthenticationServices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
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)
}
}

0 comments on commit 6b1ca16

Please sign in to comment.