@@ -550,6 +550,7 @@ export default function LayoutWrapper({ children }) {
550550 const [ isSearchOpen , setSearchOpen ] = useState ( false )
551551 const [ isMobileNavOpen , setMobileNavOpen ] = useState ( false )
552552 const [ unreadCount , setUnreadCount ] = useState ( 0 )
553+ const [ user , setUser ] = useState ( null ) // Unified user state
553554 const [ notifRefreshKey , setNotifRefreshKey ] = useState ( 0 )
554555 const [ userDetails , setUserDetails ] = useState ( null )
555556 const wsRef = useRef ( null )
@@ -566,7 +567,7 @@ export default function LayoutWrapper({ children }) {
566567 isHighlightPaused : false
567568 } )
568569 const chatActionsRef = useRef ( null )
569- const pathname = usePathname ( )
570+ const pathname = usePathname ( ) // eslint-disable-line
570571 const router = useRouter ( )
571572
572573 const skipTour = useCallback ( ( ) => {
@@ -820,7 +821,11 @@ export default function LayoutWrapper({ children }) {
820821 }
821822 ]
822823
823- const { user, error : authError , isLoading : isAuthLoading } = useUser ( )
824+ const {
825+ user : auth0User ,
826+ error : authError ,
827+ isLoading : isAuthLoading
828+ } = useUser ( )
824829
825830 const [ isLoading , setIsLoading ] = useState ( true )
826831 const [ isAllowed , setIsAllowed ] = useState ( false )
@@ -912,10 +917,10 @@ export default function LayoutWrapper({ children }) {
912917 const showNav = ! [ "/" , "/onboarding" ] . includes ( pathname )
913918
914919 useEffect ( ( ) => {
915- if ( user && posthog ) {
916- posthog . identify ( user . sub , {
917- name : user . name ,
918- email : user . email
920+ if ( auth0User && posthog ) {
921+ posthog . identify ( auth0User . sub , {
922+ name : auth0User . name ,
923+ email : auth0User . email
919924 } )
920925
921926 // --- NEW: Fetch custom properties and set PostHog groups ---
@@ -967,7 +972,7 @@ export default function LayoutWrapper({ children }) {
967972 fetchAndSetUserGroups ( )
968973 // --- END NEW ---
969974 }
970- } , [ user , posthog ] )
975+ } , [ auth0User , posthog ] )
971976
972977 useEffect ( ( ) => {
973978 const paymentStatus = searchParams . get ( "payment_status" )
@@ -1031,39 +1036,55 @@ export default function LayoutWrapper({ children }) {
10311036 return
10321037 }
10331038
1034- if ( isAuthLoading ) return
1039+ const checkStatus = async ( ) => {
1040+ // --- SELF-HOST AUTH LOGIC ---
1041+ if ( process . env . NEXT_PUBLIC_ENVIRONMENT === "selfhost" ) {
1042+ try {
1043+ const res = await fetch ( "/api/user/profile" )
1044+ if ( res . ok ) {
1045+ const selfHostUser = await res . json ( )
1046+ setUser ( selfHostUser ) // Set the unified user state
1047+ setIsAllowed ( true )
1048+ } else {
1049+ throw new Error (
1050+ "Failed to fetch self-host user profile."
1051+ )
1052+ }
1053+ } catch ( error ) {
1054+ toast . error ( error . message )
1055+ setIsAllowed ( false )
1056+ } finally {
1057+ setIsLoading ( false )
1058+ }
1059+ return
1060+ }
10351061
1036- if ( authError ) {
1037- toast . error ( "Session error. Redirecting to login." , {
1038- id : "auth-error"
1039- } )
1040- router . push ( "/auth/login" )
1041- return
1042- }
1062+ // --- AUTH0 AUTH LOGIC ---
1063+ if ( isAuthLoading ) return
10431064
1044- if ( ! user ) {
1045- // This handles the case where the user logs out.
1046- router . push ( "/auth/login" )
1047- return
1048- }
1065+ if ( authError ) {
1066+ toast . error (
1067+ `Session error: ${ authError . message } . Redirecting to login.` ,
1068+ { id : "auth-error" }
1069+ )
1070+ router . push ( "/api/auth/login" )
1071+ return
1072+ }
1073+
1074+ if ( ! auth0User ) {
1075+ router . push ( "/api/auth/login" )
1076+ return
1077+ }
1078+
1079+ setUser ( auth0User ) // Set the unified user state
10491080
1050- const checkStatus = async ( ) => {
1051- // No need to set isLoading(true) here, it's already true by default.
10521081 try {
10531082 const res = await fetch ( "/api/user/data" , { method : "POST" } )
10541083 if ( ! res . ok ) throw new Error ( "Could not verify user status." )
10551084 const result = await res . json ( )
1056- const data = result ?. data || { }
1057-
1058- const onboardingComplete = data . onboardingComplete
1059-
1060- if ( ! onboardingComplete ) {
1061- toast . error ( "Please complete onboarding first." , {
1062- id : "onboarding-check"
1063- } )
1085+ if ( ! result ?. data ?. onboardingComplete ) {
10641086 router . push ( "/onboarding" )
10651087 } else {
1066- // User is fully onboarded and profile is complete.
10671088 setIsAllowed ( true )
10681089 }
10691090 } catch ( error ) {
@@ -1075,7 +1096,7 @@ export default function LayoutWrapper({ children }) {
10751096 }
10761097
10771098 checkStatus ( )
1078- } , [ pathname , router , showNav , user , authError , isAuthLoading ] ) // Reruns on navigation
1099+ } , [ showNav , auth0User , isAuthLoading , authError , router ] )
10791100
10801101 const handleNotificationsOpen = useCallback ( ( ) => {
10811102 setNotificationsOpen ( true )
@@ -1294,8 +1315,8 @@ export default function LayoutWrapper({ children }) {
12941315 } , [ ] )
12951316
12961317 useEffect ( ( ) => {
1297- if ( showNav && userDetails ?. sub ) subscribeToPushNotifications ( )
1298- } , [ showNav , userDetails , subscribeToPushNotifications ] )
1318+ if ( showNav && user ?. sub ) subscribeToPushNotifications ( )
1319+ } , [ showNav , user , subscribeToPushNotifications ] )
12991320
13001321 // Define shortcuts after all their callback dependencies are defined
13011322 useGlobalShortcuts (
@@ -1325,14 +1346,14 @@ export default function LayoutWrapper({ children }) {
13251346 < PlanContext . Provider
13261347 value = { {
13271348 plan : (
1328- user ?. [
1349+ auth0User ?. [
13291350 `${ process . env . NEXT_PUBLIC_AUTH0_NAMESPACE } /roles`
13301351 ] || [ ]
13311352 ) . includes ( "Pro" )
13321353 ? "pro"
13331354 : "free" ,
13341355 isPro : (
1335- user ?. [
1356+ auth0User ?. [
13361357 `${ process . env . NEXT_PUBLIC_AUTH0_NAMESPACE } /roles`
13371358 ] || [ ]
13381359 ) . includes ( "Pro" ) ,
0 commit comments