From 546386dcbdf8582dd85792a2f99782ab12e9423f Mon Sep 17 00:00:00 2001 From: rene Date: Sat, 30 May 2026 18:25:23 +0200 Subject: [PATCH] =?UTF-8?q?Hunde-Cache=20leeren=20bei=20Logout/401=20?= =?UTF-8?q?=E2=80=94=20kein=20Durchschimmern=20auf=20neuen=20User?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: ActiveDogStore cached dogs + activeDogId (UserDefaults). Beim Login mit einem neuen Account waren die Hunde des vorigen Users weiter zu sehen, weil HeimView nur loadDogs() ruft wenn dogs.isEmpty. Fix: - ActiveDogStore hat jetzt reset() (dogs=[], activeDogId=0, UserDefaults gelöscht). - ActiveDogStore hört auf .userDidLogout und auf .apiUnauthorized, beides löst reset() aus. - AuthSession.logout() postet jetzt .userDidLogout. - Nach Login holt HeimView's .task automatisch die neuen Hunde (dogs.isEmpty → loadDogs). --- BanYaroGo/Auth/AuthSession.swift | 2 ++ BanYaroGo/Support/ActiveDogStore.swift | 31 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/BanYaroGo/Auth/AuthSession.swift b/BanYaroGo/Auth/AuthSession.swift index 6da8365..53fc227 100644 --- a/BanYaroGo/Auth/AuthSession.swift +++ b/BanYaroGo/Auth/AuthSession.swift @@ -55,6 +55,8 @@ final class AuthSession { userName = nil isPremium = false profile = nil + // Andere Stores benachrichtigen, damit sie ihren User-Cache leeren. + NotificationCenter.default.post(name: .userDidLogout, object: nil) } /// Fetches the full user profile from /api/auth/me. Called after login and diff --git a/BanYaroGo/Support/ActiveDogStore.swift b/BanYaroGo/Support/ActiveDogStore.swift index 9dea00e..de17034 100644 --- a/BanYaroGo/Support/ActiveDogStore.swift +++ b/BanYaroGo/Support/ActiveDogStore.swift @@ -15,6 +15,22 @@ final class ActiveDogStore { init() { self.activeDogId = UserDefaults.standard.integer(forKey: "activeDogId") + // Auf User-Wechsel hören (Logout oder 401 = Token abgelaufen), + // damit nie die Hunde des vorigen Users durchschimmern. + NotificationCenter.default.addObserver( + forName: .userDidLogout, + object: nil, + queue: .main + ) { [weak self] _ in + Task { @MainActor in self?.reset() } + } + NotificationCenter.default.addObserver( + forName: .apiUnauthorized, + object: nil, + queue: .main + ) { [weak self] _ in + Task { @MainActor in self?.reset() } + } } func loadDogs() async { @@ -34,4 +50,19 @@ final class ActiveDogStore { activeDogId = dogId UserDefaults.standard.set(dogId, forKey: "activeDogId") } + + /// Komplett zurücksetzen — wird bei Logout / 401 aufgerufen, damit kein + /// Cache vom vorigen User durchschlägt. `loadDogs()` wird beim nächsten + /// Erscheinen einer Seite (`dogs.isEmpty`) automatisch wieder gefeuert. + func reset() { + dogs = [] + activeDogId = 0 + UserDefaults.standard.removeObject(forKey: "activeDogId") + } +} + +extension Notification.Name { + /// Wird von `AuthSession.logout()` gepostet, damit abhängige Stores + /// (z.B. ActiveDogStore) ihren User-bezogenen Cache leeren. + static let userDidLogout = Notification.Name("BanYaroGo.userDidLogout") }