597787a6c9
- HTTPClient: replace isRefreshing bool with shared Task to safely coalesce concurrent 401 refresh attempts; surface JSON serialization error instead of silently dropping request body - AuthService.logout: always clear Keychain tokens via defer, even when refresh token is absent, preventing stale access token - NotificationsAPIService: remove updateAppBadge (UIKit call moved to @MainActor NotificationsViewModel); drop unused UIKit import - NotificationsViewModel: guard startPolling() against duplicate tasks; update badge directly on @MainActor instead of hopping to actor - VerifyEmailView: replace Timer (never invalidated) with async Task cancelled in .onDisappear - NotificationsView: use Text(date, style: .relative) — auto-updates without custom formatter; remove duplicate Date extension - SettingsView: handle logoutAll errors explicitly with alert instead of silently proceeding with local logout - MaydayLiveActivity/Info.plist: add NSExtensionPrincipalClass so the widget extension is discoverable by the system - Live Activity widget: replace frozen duration(from:) with Text(date, style: .timer); replace frozen relativeFormatted with Text(date, style: .relative); localize status badge to Russian Co-authored-by: robonen <26167508+robonen@users.noreply.github.com>
72 lines
2.2 KiB
Swift
72 lines
2.2 KiB
Swift
import Foundation
|
|
|
|
struct LoginResponse: Decodable {
|
|
let user: UserResponse
|
|
let tokens: TokenPair
|
|
}
|
|
|
|
struct RegisterResponse: Decodable {
|
|
let user: UserResponse
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case id, email, status, metadata, roles
|
|
case emailVerifiedAt = "email_verified_at"
|
|
case createdAt = "created_at"
|
|
case updatedAt = "updated_at"
|
|
}
|
|
|
|
init(from decoder: Decoder) throws {
|
|
user = try UserResponse(from: decoder)
|
|
}
|
|
}
|
|
|
|
struct VerifyEmailResponse: Decodable {
|
|
let user: UserResponse
|
|
}
|
|
|
|
actor AuthService {
|
|
static let shared = AuthService()
|
|
private let client = HTTPClient.shared
|
|
private let keychain = KeychainService.shared
|
|
|
|
private init() {}
|
|
|
|
func login(email: String, password: String) async throws -> UserResponse {
|
|
let response: LoginResponse = try await client.request(.login(email: email, password: password))
|
|
try keychain.saveTokens(response.tokens)
|
|
return response.user
|
|
}
|
|
|
|
func register(email: String, password: String) async throws -> UserResponse {
|
|
let response: UserResponse = try await client.request(.register(email: email, password: password))
|
|
return response
|
|
}
|
|
|
|
func verifyEmail(email: String, code: String) async throws -> UserResponse {
|
|
let response: VerifyEmailResponse = try await client.request(.verifyEmail(email: email, code: code))
|
|
return response.user
|
|
}
|
|
|
|
func resendCode(email: String) async throws {
|
|
let _: ResendCodeResponse = try await client.request(.resendCode(email: email))
|
|
}
|
|
|
|
func logout() async throws {
|
|
// Always clear local tokens, regardless of whether the network call succeeds,
|
|
// to avoid leaving a stale access token in Keychain.
|
|
defer { keychain.clearTokens() }
|
|
guard let refreshToken = keychain.loadRefreshToken() else { return }
|
|
let _: EmptyResponse = try await client.request(.logout(refreshToken: refreshToken))
|
|
}
|
|
|
|
func getMe() async throws -> UserResponse {
|
|
try await client.request(.getMe)
|
|
}
|
|
}
|
|
|
|
struct ResendCodeResponse: Decodable {
|
|
let message: String
|
|
}
|
|
|
|
struct EmptyResponse: Decodable {}
|