diff --git a/Sources/DDGSync/internal/AccountManager.swift b/Sources/DDGSync/internal/AccountManager.swift index 2469372d6..bcec3d38d 100644 --- a/Sources/DDGSync/internal/AccountManager.swift +++ b/Sources/DDGSync/internal/AccountManager.swift @@ -143,17 +143,12 @@ struct AccountManager: AccountManaging { throw SyncError.noToken } - let devices = try await fetchDevicesForAccount(account) - - // Logout other devices first or the call will fail - for device in devices.filter({ $0.id != account.deviceId }) { - try await logout(deviceId: device.id, token: token) - } + let request = api.createAuthenticatedJSONRequest(url: endpoints.deleteAccount, method: .POST, authToken: token) + let result = try await request.execute() + let statusCode = result.response.statusCode - // This is the last device, the backend will perge the data after this - // An explicit delete account endpoint might be better though - if let thisDevice = devices.first(where: { $0.id == account.deviceId }) { - try await logout(deviceId: thisDevice.id, token: token) + guard statusCode == 204 else { + throw SyncError.unexpectedStatusCode(statusCode) } } diff --git a/Sources/DDGSync/internal/Endpoints.swift b/Sources/DDGSync/internal/Endpoints.swift index 137506aa3..161394dad 100644 --- a/Sources/DDGSync/internal/Endpoints.swift +++ b/Sources/DDGSync/internal/Endpoints.swift @@ -23,9 +23,10 @@ class Endpoints { private(set) var baseURL: URL private(set) var signup: URL + private(set) var connect: URL private(set) var login: URL private(set) var logoutDevice: URL - private(set) var connect: URL + private(set) var deleteAccount: URL private(set) var syncGet: URL private(set) var syncPatch: URL @@ -47,6 +48,7 @@ class Endpoints { signup = baseURL.appendingPathComponent("sync/signup") login = baseURL.appendingPathComponent("sync/login") logoutDevice = baseURL.appendingPathComponent("sync/logout-device") + deleteAccount = baseURL.appendingPathComponent("sync/delete-account") connect = baseURL.appendingPathComponent("sync/connect") syncGet = baseURL.appendingPathComponent("sync") @@ -63,6 +65,7 @@ extension Endpoints { signup = baseURL.appendingPathComponent("sync/signup") login = baseURL.appendingPathComponent("sync/login") logoutDevice = baseURL.appendingPathComponent("sync/logout-device") + deleteAccount = baseURL.appendingPathComponent("sync/delete-account") connect = baseURL.appendingPathComponent("sync/connect") syncGet = baseURL.appendingPathComponent("sync") diff --git a/Sources/DDGSync/internal/RemoteAPIRequestCreatingExtensions.swift b/Sources/DDGSync/internal/RemoteAPIRequestCreatingExtensions.swift index fd196fdbc..3149d5339 100644 --- a/Sources/DDGSync/internal/RemoteAPIRequestCreatingExtensions.swift +++ b/Sources/DDGSync/internal/RemoteAPIRequestCreatingExtensions.swift @@ -40,7 +40,7 @@ extension RemoteAPIRequestCreating { func createAuthenticatedJSONRequest(url: URL, method: HTTPRequestMethod, authToken: String, - json: Data, + json: Data? = nil, headers: [String: String] = [:], parameters: [String: String] = [:]) -> HTTPRequesting { var headers = headers