@@ -11,7 +11,7 @@ import { useNavigate } from 'react-router-dom';
1111import { authClient } from '../lib/auth-client' ;
1212import { fetchIdentityUser , switchUserContext } from '@/api/Auth/auth' ;
1313
14- interface User {
14+ export interface User {
1515 id : string ;
1616 activeUserId : string ;
1717 email : string ;
@@ -21,6 +21,16 @@ interface User {
2121 mfaEmailEnabled : boolean ;
2222}
2323
24+ interface ExtendedSessionUser {
25+ id : string ;
26+ email : string ;
27+ name : string | null ;
28+ activeUserId ?: string ;
29+ role ?: string ;
30+ twoFactorEnabled ?: boolean ;
31+ mfaEmailEnabled ?: boolean ;
32+ }
33+
2434interface AuthContextType {
2535 user : User | null ;
2636 loading : boolean ;
@@ -47,8 +57,7 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({
4757 const [ user , setUser ] = useState < User | null > ( null ) ;
4858 const [ isSyncing , setIsSyncing ] = useState ( true ) ; // Track initial hydration
4959 const navigate = useNavigate ( ) ;
50- // eslint-disable-next-line @typescript-eslint/no-explicit-any
51- const prevSessionRef = React . useRef < any > ( null ) ;
60+ const prevSessionRef = React . useRef < typeof session > ( null ) ;
5261
5362 // Only show global loading during initial hydration (isSyncing).
5463 // Ignoring sessionLoading avoids unmounting components (like Auth/MFA) during background re-fetches.
@@ -60,34 +69,21 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({
6069 useEffect ( ( ) => {
6170 // Log when session changes to identify refresh triggers
6271 if ( session !== prevSessionRef . current ) {
63- const prevUser = prevSessionRef . current ?. user ?. id ;
64- const newUser = session ?. user ?. id ;
65- const isSameUser = prevUser === newUser ;
66-
67- // console.log('[Auth Hook] Session update detected:', {
68- // timestamp: new Date().toLocaleTimeString(),
69- // isSameUser,
70- // prevUserId: prevUser,
71- // newUserId: newUser,
72- // trigger: isSameUser ? 'POTENTIAL UNWANTED REFRESH' : 'USER CHANGED',
73- // });
74-
7572 prevSessionRef . current = session ;
7673 }
7774
7875 // Only process if session has actual user data AND it's different from what we have
7976 if ( session ?. user && ( ! user || user . id !== session . user . id ) ) {
80- const sessionUser = {
81- id : session . user . id ,
82- // eslint-disable-next-line @typescript-eslint/no-explicit-any
83- activeUserId : ( session . user as any ) . activeUserId || session . user . id ,
84- email : session . user . email ,
85- fullName : session . user . name || null ,
86- // eslint-disable-next-line @typescript-eslint/no-explicit-any
87- role : ( session . user as any ) . role || 'user' ,
88- twoFactorEnabled : ! ! session . user . twoFactorEnabled ,
89- // eslint-disable-next-line @typescript-eslint/no-explicit-any
90- mfaEmailEnabled : ! ! ( session . user as any ) . mfaEmailEnabled ,
77+ const extUser = session . user as unknown as ExtendedSessionUser ;
78+
79+ const sessionUser : User = {
80+ id : extUser . id ,
81+ activeUserId : extUser . activeUserId || extUser . id ,
82+ email : extUser . email ,
83+ fullName : extUser . name || null ,
84+ role : extUser . role || 'user' ,
85+ twoFactorEnabled : ! ! extUser . twoFactorEnabled ,
86+ mfaEmailEnabled : ! ! extUser . mfaEmailEnabled ,
9187 } ;
9288
9389 //console.log('[Auth Hook] Setting user state from session:', sessionUser.id);
@@ -96,8 +92,7 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({
9692 // Fetch Authoritative Data (Active Context)
9793 // This runs on every session update to ensure we are strictly in sync with the backend.
9894 fetchIdentityUser ( )
99- // eslint-disable-next-line @typescript-eslint/no-explicit-any
100- . then ( ( realUserData : any ) => {
95+ . then ( ( realUserData ) => {
10196 setUser ( ( prev ) => {
10297 if ( ! prev ) return prev ;
10398 if (
@@ -127,7 +122,6 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({
127122 setIsSyncing ( false ) ;
128123 } else if ( session ?. user && user && user . id === session . user . id ) {
129124 // Same user - just update 2FA status if changed
130- // console.log('[Auth Hook] Session re-poll detected, skipping unnecessary update for same user');
131125 setIsSyncing ( false ) ;
132126 }
133127 } , [ session , user ] ) ;
0 commit comments