88 SET_CURRENT_USER ,
99 SET_HEADER_DATA ,
1010 START_FORCE_LOGOUT ,
11+ STOP_FORCE_LOGOUT ,
1112} from '../constants/auth' ;
1213
1314const { tokenKey } = config ;
@@ -22,6 +23,21 @@ export const setHeaderData = data => ({
2223 payload : data ,
2324} ) ;
2425
26+ /**
27+ * Stops any active force logout timer and clears related state
28+ */
29+ export const stopForceLogout = ( ) => ( dispatch , getState ) => {
30+ const { auth } = getState ( ) ;
31+ if ( auth ?. timerId ) {
32+ try {
33+ clearTimeout ( auth . timerId ) ;
34+ } catch ( e ) {
35+ // Timer already cleared or invalid
36+ }
37+ }
38+ dispatch ( { type : STOP_FORCE_LOGOUT } ) ;
39+ } ;
40+
2541export const loginUser = credentials => dispatch => {
2642 return httpService
2743 . post ( ENDPOINTS . LOGIN , credentials )
@@ -33,6 +49,8 @@ export const loginUser = credentials => dispatch => {
3349 localStorage . setItem ( tokenKey , res . data . token ) ;
3450 httpService . setjwt ( res . data . token ) ;
3551 const decoded = jwtDecode ( res . data . token ) ;
52+ // Ensure any existing timers from a previous session are cleared
53+ dispatch ( stopForceLogout ( ) ) ;
3654 dispatch ( setCurrentUser ( decoded ) ) ;
3755 return { success : true } ;
3856 } )
@@ -98,6 +116,8 @@ export const getHeaderData = userId => {
98116} ;
99117
100118export const logoutUser = ( ) => dispatch => {
119+ // Clear any active force-logout timer before logging out
120+ dispatch ( stopForceLogout ( ) ) ;
101121 localStorage . removeItem ( tokenKey ) ;
102122 httpService . setjwt ( false ) ;
103123 dispatch ( setCurrentUser ( null ) ) ;
@@ -134,6 +154,9 @@ export const startForceLogout = (delayMs = 20000) => (dispatch, getState) => {
134154 // eslint-disable-next-line no-console
135155 console . error ( 'Error acknowledging permissions during force logout:' , error ) ;
136156 } finally {
157+ // Set flag to indicate user was force logged out due to permission changes
158+ // This helps distinguish "force logged out" vs "first login after permission change"
159+ sessionStorage . setItem ( 'wasForceLoggedOut' , 'true' ) ;
137160 dispatch ( logoutUser ( ) ) ;
138161 }
139162 } , delayMs ) ;
0 commit comments