@@ -16,9 +16,10 @@ import ReviewCompletedContracts from "./EmployerPages/ReviewCompletedContracts";
1616import Job from "./EmployerPages/Job" ;
1717import ReviewApplications from "./EmployerPages/ReviewApplications" ;
1818import EmployeeJobsPage from "./EmployeePages/EmployeeJobsPage" ;
19- import { useEffect , useState } from "react" ;
19+ import { useEffect , useState , useRef } from "react" ;
2020import UserProfile from "./pages/UserProfile" ;
2121import ClosedContracts from "./EmployerPages/ClosedContracts" ;
22+ import apiService from "./services/api" ;
2223
2324const enhancedEmployeeView = {
2425 type : SdkViewType . Login ,
@@ -42,43 +43,162 @@ import ProtectedRoute from "./components/ProtectedRoute";
4243
4344// Inner component to handle redirects based on auth state
4445const AppContent = ( ) => {
45- const { user, isAuthenticated, isLoading } = useDynamicContext ( ) ;
46+ const { user, isAuthenticated, isLoading, primaryWallet } = useDynamicContext ( ) ;
4647 const navigate = useNavigate ( ) ;
4748 const location = useLocation ( ) ;
4849 const [ hasRedirected , setHasRedirected ] = useState ( false ) ;
50+ const [ checkingProfile , setCheckingProfile ] = useState ( false ) ;
51+
52+ // Track previous user ID to detect new logins
53+ const prevUserIdRef = useRef ( user ?. id ) ;
54+
55+ useEffect ( ( ) => {
56+ // Reset redirect flags only when user ID actually changes (new login)
57+ if ( user ?. id && user . id !== prevUserIdRef . current ) {
58+ setHasRedirected ( false ) ;
59+ setCheckingProfile ( false ) ;
60+ prevUserIdRef . current = user . id ;
61+ console . log ( 'New user login detected, reset redirect flags:' , user . id ) ;
62+ }
63+ } , [ user ?. id ] ) ;
4964
5065 useEffect ( ( ) => {
5166 // Wait for loading to complete and user to be available
52- if ( ! isLoading && isAuthenticated && user && location . pathname === '/' && ! hasRedirected ) {
53- const isNew = user . newUser === true ;
67+ // Redirect authenticated users from landing page or any public page
68+ const isPublicPage = location . pathname === '/' || location . pathname === '/about-us' ;
69+ // If user exists, consider authenticated (user object is the source of truth)
70+ // isLoading can be undefined initially, so treat undefined as "not loading"
71+ const loadingComplete = isLoading !== true ;
72+ const authenticated = user !== null && user !== undefined ; // User object exists = authenticated
73+ const shouldCheck = loadingComplete && authenticated && user && isPublicPage && ! hasRedirected && ! checkingProfile ;
74+
75+ console . log ( 'AppContent effect check:' , {
76+ isLoading,
77+ isAuthenticated,
78+ hasUser : ! ! user ,
79+ isPublicPage,
80+ hasRedirected,
81+ checkingProfile,
82+ loadingComplete,
83+ authenticated,
84+ shouldCheck
85+ } ) ;
86+
87+ if ( shouldCheck ) {
5488 const userRole =
5589 user ?. metadata ?. role ||
5690 localStorage . getItem ( 'persistedUserRole' ) ||
5791 localStorage . getItem ( 'userRole' ) ||
5892 localStorage . getItem ( 'pendingRole' ) ;
5993
60- console . log ( 'AppContent: User authenticated, role:' , userRole , 'isNew :' , isNew , 'user :' , user ) ;
94+ console . log ( 'AppContent: User authenticated, role:' , userRole , 'user :' , user , 'primaryWallet :' , primaryWallet ?. address , 'location:' , location . pathname ) ;
6195
6296 setHasRedirected ( true ) ;
97+ setCheckingProfile ( true ) ;
6398
64- // Add a small delay to ensure user state is fully set
65- setTimeout ( ( ) => {
66- if ( isNew ) {
67- console . log ( 'Redirecting new user to /user-profile' ) ;
68- navigate ( '/user-profile' , { replace : true } ) ;
69- } else if ( userRole === 'employee' ) {
70- console . log ( 'Redirecting employee to /employeeDashboard' ) ;
71- navigate ( '/employeeDashboard' , { replace : true } ) ;
72- } else if ( userRole === 'employer' ) {
73- console . log ( 'Redirecting employer to /employerDashboard' ) ;
74- navigate ( '/employerDashboard' , { replace : true } ) ;
75- } else {
76- console . warn ( 'No role found, redirecting to /user-profile' ) ;
77- navigate ( '/user-profile' , { replace : true } ) ;
99+ // Check if user already has a profile in backend (works for both email and phone login)
100+ const checkProfileAndRedirect = async ( ) => {
101+ try {
102+ // First check Dynamic Labs newUser flag - if new, skip DB check and redirect to profile
103+ const isNew = user . newUser === true ;
104+
105+ if ( isNew ) {
106+ console . log ( 'New user detected by Dynamic Labs, redirecting to /user-profile (skipping DB check)' ) ;
107+ window . location . href = '/user-profile' ;
108+ return ;
109+ }
110+
111+ // User is not new according to Dynamic Labs - check database for existing profile
112+ let profileExists = false ;
113+ let detectedRole = userRole ;
114+ const walletAddress = primaryWallet ?. address ;
115+
116+ console . log ( 'Existing user, checking profile in database with wallet:' , walletAddress ) ;
117+
118+ // If we have a wallet address, check if profile exists in backend
119+ if ( walletAddress ) {
120+ try {
121+ // Check employee profile
122+ const empResponse = await apiService . getEmployeeByWallet ( walletAddress ) ;
123+ if ( empResponse ?. data ) {
124+ profileExists = true ;
125+ detectedRole = 'employee' ;
126+ localStorage . setItem ( 'persistedUserRole' , 'employee' ) ;
127+ console . log ( 'Found existing employee profile in backend' ) ;
128+ }
129+ } catch ( empError ) {
130+ // Not an employee, check employer
131+ try {
132+ const empResponse = await apiService . getEmployerByWallet ( walletAddress ) ;
133+ if ( empResponse ?. data ) {
134+ profileExists = true ;
135+ detectedRole = 'employer' ;
136+ localStorage . setItem ( 'persistedUserRole' , 'employer' ) ;
137+ console . log ( 'Found existing employer profile in backend' ) ;
138+ }
139+ } catch ( empError2 ) {
140+ // No profile found
141+ console . log ( 'No existing profile found in backend' ) ;
142+ }
143+ }
144+ } else {
145+ console . log ( 'No wallet address available, using role-based redirect' ) ;
146+ }
147+
148+ // If profile exists, redirect to dashboard (same flow as email login)
149+ if ( profileExists ) {
150+ if ( detectedRole === 'employee' ) {
151+ console . log ( 'Profile exists, redirecting employee to /employeeDashboard' ) ;
152+ window . location . href = '/employeeDashboard' ;
153+ } else if ( detectedRole === 'employer' ) {
154+ console . log ( 'Profile exists, redirecting employer to /employerDashboard' ) ;
155+ window . location . href = '/employerDashboard' ;
156+ } else {
157+ console . log ( 'Profile exists but no role, redirecting to /user-profile' ) ;
158+ window . location . href = '/user-profile' ;
159+ }
160+ return ;
161+ }
162+
163+ // No profile found in DB but user is not new - redirect based on role
164+ console . log ( 'No profile found in DB, but user is not new. userRole:' , userRole ) ;
165+
166+ if ( userRole === 'employee' ) {
167+ console . log ( 'Redirecting employee to /employeeDashboard' ) ;
168+ window . location . href = '/employeeDashboard' ;
169+ } else if ( userRole === 'employer' ) {
170+ console . log ( 'Redirecting employer to /employerDashboard' ) ;
171+ window . location . href = '/employerDashboard' ;
172+ } else {
173+ console . warn ( 'No role found, redirecting to /user-profile' ) ;
174+ window . location . href = '/user-profile' ;
175+ }
176+ } catch ( error ) {
177+ console . error ( 'Error checking profile:' , error ) ;
178+ // On error, fall back to original logic (same as email login)
179+ const isNew = user . newUser === true ;
180+ if ( isNew ) {
181+ console . log ( 'Error occurred, redirecting new user to /user-profile' ) ;
182+ navigate ( '/user-profile' , { replace : true } ) ;
183+ } else if ( userRole === 'employee' ) {
184+ console . log ( 'Error occurred, redirecting employee to /employeeDashboard' ) ;
185+ navigate ( '/employeeDashboard' , { replace : true } ) ;
186+ } else if ( userRole === 'employer' ) {
187+ console . log ( 'Error occurred, redirecting employer to /employerDashboard' ) ;
188+ navigate ( '/employerDashboard' , { replace : true } ) ;
189+ } else {
190+ console . log ( 'Error occurred, redirecting to /user-profile' ) ;
191+ navigate ( '/user-profile' , { replace : true } ) ;
192+ }
193+ } finally {
194+ setCheckingProfile ( false ) ;
78195 }
79- } , 300 ) ;
196+ } ;
197+
198+ // Add a small delay to ensure user state is fully set, then check profile
199+ setTimeout ( checkProfileAndRedirect , 300 ) ;
80200 }
81- } , [ isLoading , isAuthenticated , user , location . pathname , navigate , hasRedirected ] ) ;
201+ } , [ isLoading , isAuthenticated , user , primaryWallet , location . pathname , navigate , hasRedirected , checkingProfile ] ) ;
82202
83203 return null ;
84204} ;
@@ -165,7 +285,6 @@ const App = () => {
165285 events : {
166286 onAuthSuccess : ( args ) => {
167287 console . log ( 'Auth success event fired:' , args ) ;
168- const isNew = args ?. user ?. newUser === true ;
169288
170289 // Get role from multiple sources
171290 const userRole =
@@ -174,27 +293,15 @@ const App = () => {
174293 localStorage . getItem ( 'userRole' ) ||
175294 localStorage . getItem ( 'pendingRole' ) ;
176295
177- console . log ( 'User role detected:' , userRole , 'isNew:' , isNew ) ;
296+ console . log ( 'User role detected:' , userRole ) ;
178297
179- // Use a longer delay to ensure user state is fully loaded before redirect
298+ // Trigger a small delay to let AppContent detect the auth state change
299+ // AppContent will handle the actual redirect with profile checking
180300 setTimeout ( ( ) => {
181- if ( isNew ) {
182- console . log ( 'Redirecting new user to /user-profile' ) ;
183- window . location . href = '/user-profile' ;
184- return ;
185- }
186-
187- if ( userRole === 'employee' ) {
188- console . log ( 'Redirecting employee to /employeeDashboard' ) ;
189- window . location . href = '/employeeDashboard' ;
190- } else if ( userRole === 'employer' ) {
191- console . log ( 'Redirecting employer to /employerDashboard' ) ;
192- window . location . href = '/employerDashboard' ;
193- } else {
194- console . warn ( 'No role found, redirecting to /user-profile' ) ;
195- window . location . href = '/user-profile' ;
196- }
197- } , 500 ) ;
301+ // Force a re-render by updating a state that AppContent depends on
302+ // The AppContent useEffect will run again and detect the authenticated state
303+ console . log ( 'onAuthSuccess: Waiting for AppContent to handle redirect...' ) ;
304+ } , 100 ) ;
198305 } ,
199306 } ,
200307 } }
0 commit comments