All files / store listenerMiddleware.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 1/1
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29              1x                             1x     1x      
import {createListenerMiddleware} from '@reduxjs/toolkit';
import {api} from '../services/api';
import {logout} from '../features/auth/authSlice';
 
/**
 * Side-effect middleware for cross-slice reactions.
 */
export const listenerMiddleware = createListenerMiddleware();
 
/**
 * Clear all cached RTK Query data whenever a user logs out (FSC-59).
 *
 * The auth slice is persisted, but the API cache is in-memory only. Without
 * this, logging out and logging back in *in the same tab* leaves the previous
 * user's cached query results (e.g. their loans) in the store until something
 * triggers a refetch — which is why a hard refresh "fixed" it. Resetting the
 * cache here guarantees the next user starts from a clean slate.
 *
 * Listening on the `logout` action (rather than dispatching the reset inline at
 * each logout site) also covers the automatic logout in the re-auth wrapper
 * when a token refresh fails.
 */
listenerMiddleware.startListening({
	actionCreator: logout,
	effect: (_action, listenerApi) => {
		listenerApi.dispatch(api.util.resetApiState());
	}
});