feat: implement full FCM token cleanup on logout by deleting both remote and local tokens
This commit is contained in:
@@ -254,16 +254,26 @@ class PushNotificationService {
|
|||||||
_dispatchTap(Map<String, dynamic>.from(message.data));
|
_dispatchTap(Map<String, dynamic>.from(message.data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unregister FCM token from backend (call on logout).
|
/// Unregister FCM token from backend and delete locally (call on logout).
|
||||||
Future<void> unregister() async {
|
Future<void> unregister() async {
|
||||||
if (_currentToken == null) return;
|
// Remove from backend so it stops sending push to this device
|
||||||
|
if (_currentToken != null) {
|
||||||
try {
|
try {
|
||||||
await _repo.removeFcmToken(_currentToken!);
|
await _repo.removeFcmToken(_currentToken!);
|
||||||
debugPrint('[FCM] Token unregistered from backend');
|
debugPrint('[FCM] Token unregistered from backend');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint('[FCM] Failed to unregister token: $e');
|
debugPrint('[FCM] Failed to unregister token: $e');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
_currentToken = null;
|
_currentToken = null;
|
||||||
|
|
||||||
|
// Delete the local FCM token so Firebase stops delivering notifications
|
||||||
|
try {
|
||||||
|
await _messaging?.deleteToken();
|
||||||
|
debugPrint('[FCM] Local FCM token deleted');
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint('[FCM] Failed to delete local token: $e');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Re-register token after login.
|
/// Re-register token after login.
|
||||||
|
|||||||
@@ -78,6 +78,8 @@ class AuthNotifier extends StateNotifier<AuthState> {
|
|||||||
// when token refresh fails
|
// when token refresh fails
|
||||||
ApiClient.onForceLogout = () {
|
ApiClient.onForceLogout = () {
|
||||||
SocketService().disconnect();
|
SocketService().disconnect();
|
||||||
|
// Delete local FCM token so push notifications stop
|
||||||
|
PushNotificationService().unregister();
|
||||||
// Only reset if still authenticated — avoid showing errors on login screen
|
// Only reset if still authenticated — avoid showing errors on login screen
|
||||||
if (state.status == AuthStatus.authenticated) {
|
if (state.status == AuthStatus.authenticated) {
|
||||||
state = const AuthState();
|
state = const AuthState();
|
||||||
@@ -121,12 +123,13 @@ class AuthNotifier extends StateNotifier<AuthState> {
|
|||||||
state = const AuthState();
|
state = const AuthState();
|
||||||
// Disconnect socket immediately
|
// Disconnect socket immediately
|
||||||
SocketService().disconnect();
|
SocketService().disconnect();
|
||||||
// Clear tokens BEFORE any API calls to prevent 401 errors during logout
|
// Unregister FCM BEFORE clearing tokens — backend call needs auth header,
|
||||||
await SecureStorage.clearTokens();
|
// and deleteToken() stops Firebase from delivering to this device
|
||||||
// Best-effort cleanup — tokens already cleared, ignore any errors
|
|
||||||
try {
|
try {
|
||||||
await PushNotificationService().unregister();
|
await PushNotificationService().unregister();
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
|
// Logout API call + clear local tokens
|
||||||
|
await _repository.logout();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> login({
|
Future<void> login({
|
||||||
|
|||||||
Reference in New Issue
Block a user