Skip to content

Commit

Permalink
refactor: use timeIntervalSince for verifying token validity
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Apr 26, 2024
1 parent ac1a8a1 commit 900d26c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Sources/Auth/Internal/SessionStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct StoredSession: Codable {
var expirationDate: Date

var isValid: Bool {
expirationDate > Date().addingTimeInterval(60)
expirationDate.timeIntervalSince(Date()) > 60
}

init(session: Session, expirationDate: Date? = nil) {
Expand Down
44 changes: 22 additions & 22 deletions Sources/Auth/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,28 +195,28 @@ public struct User: Codable, Hashable, Identifiable, Sendable {

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(UUID.self, forKey: .id)
self.appMetadata = try container.decode([String : AnyJSON].self, forKey: .appMetadata)
self.userMetadata = try container.decode([String : AnyJSON].self, forKey: .userMetadata)
self.aud = try container.decode(String.self, forKey: .aud)
self.confirmationSentAt = try container.decodeIfPresent(Date.self, forKey: .confirmationSentAt)
self.recoverySentAt = try container.decodeIfPresent(Date.self, forKey: .recoverySentAt)
self.emailChangeSentAt = try container.decodeIfPresent(Date.self, forKey: .emailChangeSentAt)
self.newEmail = try container.decodeIfPresent(String.self, forKey: .newEmail)
self.invitedAt = try container.decodeIfPresent(Date.self, forKey: .invitedAt)
self.actionLink = try container.decodeIfPresent(String.self, forKey: .actionLink)
self.email = try container.decodeIfPresent(String.self, forKey: .email)
self.phone = try container.decodeIfPresent(String.self, forKey: .phone)
self.createdAt = try container.decode(Date.self, forKey: .createdAt)
self.confirmedAt = try container.decodeIfPresent(Date.self, forKey: .confirmedAt)
self.emailConfirmedAt = try container.decodeIfPresent(Date.self, forKey: .emailConfirmedAt)
self.phoneConfirmedAt = try container.decodeIfPresent(Date.self, forKey: .phoneConfirmedAt)
self.lastSignInAt = try container.decodeIfPresent(Date.self, forKey: .lastSignInAt)
self.role = try container.decodeIfPresent(String.self, forKey: .role)
self.updatedAt = try container.decode(Date.self, forKey: .updatedAt)
self.identities = try container.decodeIfPresent([UserIdentity].self, forKey: .identities)
self.isAnonymous = try container.decodeIfPresent(Bool.self, forKey: .isAnonymous) ?? false
self.factors = try container.decodeIfPresent([Factor].self, forKey: .factors)
id = try container.decode(UUID.self, forKey: .id)
appMetadata = try container.decode([String: AnyJSON].self, forKey: .appMetadata)
userMetadata = try container.decode([String: AnyJSON].self, forKey: .userMetadata)
aud = try container.decode(String.self, forKey: .aud)
confirmationSentAt = try container.decodeIfPresent(Date.self, forKey: .confirmationSentAt)
recoverySentAt = try container.decodeIfPresent(Date.self, forKey: .recoverySentAt)
emailChangeSentAt = try container.decodeIfPresent(Date.self, forKey: .emailChangeSentAt)
newEmail = try container.decodeIfPresent(String.self, forKey: .newEmail)
invitedAt = try container.decodeIfPresent(Date.self, forKey: .invitedAt)
actionLink = try container.decodeIfPresent(String.self, forKey: .actionLink)
email = try container.decodeIfPresent(String.self, forKey: .email)
phone = try container.decodeIfPresent(String.self, forKey: .phone)
createdAt = try container.decode(Date.self, forKey: .createdAt)
confirmedAt = try container.decodeIfPresent(Date.self, forKey: .confirmedAt)
emailConfirmedAt = try container.decodeIfPresent(Date.self, forKey: .emailConfirmedAt)
phoneConfirmedAt = try container.decodeIfPresent(Date.self, forKey: .phoneConfirmedAt)
lastSignInAt = try container.decodeIfPresent(Date.self, forKey: .lastSignInAt)
role = try container.decodeIfPresent(String.self, forKey: .role)
updatedAt = try container.decode(Date.self, forKey: .updatedAt)
identities = try container.decodeIfPresent([UserIdentity].self, forKey: .identities)
isAnonymous = try container.decodeIfPresent(Bool.self, forKey: .isAnonymous) ?? false
factors = try container.decodeIfPresent([Factor].self, forKey: .factors)
}
}

Expand Down
5 changes: 0 additions & 5 deletions Tests/AuthTests/SessionManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ final class SessionManagerTests: XCTestCase {
.init(session: .validSession)
}

Current.sessionRefresher.refreshSession = { _ in
XCTFail("Should not call refreshSession")
return .expiredSession
}

let sut = SessionManager.live

let session = try await sut.session()
Expand Down

0 comments on commit 900d26c

Please sign in to comment.