Skip to content

Commit

Permalink
fix(auth): add missing is_anonymous field
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Apr 26, 2024
1 parent c29ac72 commit ac1a8a1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
16 changes: 8 additions & 8 deletions Sources/Auth/Internal/SessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ private actor _DefaultSessionManager {
return try await task.value
}

guard let currentSession = try storage.getSession() else {
throw AuthError.sessionNotFound
}

if currentSession.isValid || !shouldValidateExpiration {
return currentSession.session
}

task = Task {
defer { task = nil }

guard let currentSession = try storage.getSession() else {
throw AuthError.sessionNotFound
}

if currentSession.isValid || !shouldValidateExpiration {
return currentSession.session
}

let session = try await sessionRefresher.refreshSession(currentSession.session.refreshToken)
try update(session)
return session
Expand Down
29 changes: 29 additions & 0 deletions Sources/Auth/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public struct User: Codable, Hashable, Identifiable, Sendable {
public var role: String?
public var updatedAt: Date
public var identities: [UserIdentity]?
public var isAnonymous: Bool
public var factors: [Factor]?

public init(
Expand All @@ -165,6 +166,7 @@ public struct User: Codable, Hashable, Identifiable, Sendable {
role: String? = nil,
updatedAt: Date,
identities: [UserIdentity]? = nil,
isAnonymous: Bool = false,
factors: [Factor]? = nil
) {
self.id = id
Expand All @@ -187,8 +189,35 @@ public struct User: Codable, Hashable, Identifiable, Sendable {
self.role = role
self.updatedAt = updatedAt
self.identities = identities
self.isAnonymous = isAnonymous
self.factors = factors
}

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)
}
}

public struct UserIdentity: Codable, Hashable, Identifiable, Sendable {
Expand Down
1 change: 1 addition & 0 deletions Tests/AuthTests/Resources/local-storage.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"user_id" : "859F402D-B3DE-4105-A1B9-932836D9193B"
}
],
"is_anonymous" : false,
"phone" : "",
"role" : "authenticated",
"updated_at" : "2022-04-09T11:57:01Z",
Expand Down
5 changes: 5 additions & 0 deletions Tests/AuthTests/SessionManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ 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 ac1a8a1

Please sign in to comment.