Skip to content

Commit

Permalink
Changing Type in Session Init just to avoid inside conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
LetoFranco committed Jan 27, 2023
1 parent 6b1ca16 commit e49f910
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
22 changes: 10 additions & 12 deletions ios-base/Common/Models/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
}
}
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.isSessionValid
) {
UserDataManager.currentUser = User(
dictionary: response["user"] as? [String: Any] ?? [:]
)
if let headers = headers as? [String: String] {
SessionManager.currentSession = Session(headers: headers)
}
}
}

0 comments on commit e49f910

Please sign in to comment.