@@ -75,13 +75,45 @@ const AuthManager = () => {
7575 }
7676 } , [ ] ) ;
7777
78+ // Common logic to complete login from a JWT issued by the backend
79+ const completeLoginFromToken = ( serverJWT : string ) => {
80+ setCookie ( 'jwt' , serverJWT ) ;
81+ const decoded : any = jwtDecode ( serverJWT ) ;
82+ setId ( decoded . id ) ;
83+ localStorage . setItem ( 'userId' , decoded . id ) ;
84+ localStorage . setItem ( 'userType' , decoded . userType ) ;
85+ setAuthToken ( serverJWT ) ;
86+
87+ // Refresh user data
88+ const refreshFunc = createRefresh ( decoded . id , decoded . userType , serverJWT ) ;
89+ refreshFunc ( ) ;
90+ setRefreshUser ( ( ) => refreshFunc ) ;
91+ setSignedIn ( true ) ;
92+
93+ // Navigate to appropriate dashboard based on userType
94+ if ( decoded . userType === 'Admin' ) {
95+ navigate ( '/admin/home' , { replace : true } ) ;
96+ } else if ( decoded . userType === 'Driver' ) {
97+ navigate ( '/driver/rides' , { replace : true } ) ;
98+ } else if ( decoded . userType === 'Rider' ) {
99+ navigate ( '/rider/schedule' , { replace : true } ) ;
100+ } else {
101+ // Invalid userType - this should never happen if backend is working correctly
102+ setSsoError ( 'Invalid user type received. Please contact support.' ) ;
103+ logout ( ) ;
104+ }
105+ } ;
106+
78107 // SSO Callback handler - fetches profile and JWT after successful SSO login
79- const handleSSOCallback = async ( ) => {
108+ // This is now primarily a fallback for environments where server-side
109+ // sessions are same-site (e.g., local development). In production, we
110+ // prefer the stateless JWT passed via the URL query parameter.
111+ const handleSSOCallback = async ( event ?: React . FormEvent < HTMLFormElement > ) => {
80112 try {
81113 const response = await fetch (
82114 `${ process . env . REACT_APP_SERVER_URL } /api/sso/profile` ,
83115 {
84- credentials : 'include' , // CRITICAL: Sends session cookie
116+ credentials : 'include' , // Send session cookie
85117 }
86118 ) ;
87119
@@ -93,40 +125,8 @@ const AuthManager = () => {
93125 const { user : ssoUser , token : serverJWT } = data ;
94126
95127 if ( serverJWT && ssoUser ) {
96- // Store JWT in encrypted cookie (matching Google OAuth pattern)
97- setCookie ( 'jwt' , serverJWT ) ;
98-
99- // Decode JWT to get user info
100- const decoded : any = jwtDecode ( serverJWT ) ;
101-
102- // Set auth state
103- setId ( decoded . id ) ;
104- localStorage . setItem ( 'userId' , decoded . id ) ;
105- localStorage . setItem ( 'userType' , decoded . userType ) ;
106- setAuthToken ( serverJWT ) ;
107-
108- // Refresh user data
109- const refreshFunc = createRefresh (
110- decoded . id ,
111- decoded . userType ,
112- serverJWT
113- ) ;
114- refreshFunc ( ) ;
115- setRefreshUser ( ( ) => refreshFunc ) ;
116- setSignedIn ( true ) ;
117-
118- // Navigate to appropriate dashboard based on userType
119- if ( decoded . userType === 'Admin' ) {
120- navigate ( '/admin/home' , { replace : true } ) ;
121- } else if ( decoded . userType === 'Driver' ) {
122- navigate ( '/driver/rides' , { replace : true } ) ;
123- } else if ( decoded . userType === 'Rider' ) {
124- navigate ( '/rider/schedule' , { replace : true } ) ;
125- } else {
126- // Invalid userType - this should never happen if backend is working correctly
127- setSsoError ( 'Invalid user type received. Please contact support.' ) ;
128- logout ( ) ;
129- }
128+ // Reuse common JWT login logic
129+ completeLoginFromToken ( serverJWT ) ;
130130 } else {
131131 setSsoError ( 'Failed to complete SSO login. Please try again.' ) ;
132132 logout ( ) ;
@@ -142,6 +142,7 @@ const AuthManager = () => {
142142 useEffect ( ( ) => {
143143 const authParam = searchParams . get ( 'auth' ) ;
144144 const errorParam = searchParams . get ( 'error' ) ;
145+ const tokenParam = searchParams . get ( 'token' ) ;
145146
146147 if ( errorParam ) {
147148 // Handle user_not_found specially - fetch unregistered user info
@@ -185,8 +186,12 @@ const AuthManager = () => {
185186 }
186187
187188 if ( authParam === 'sso_success' ) {
188- // Fetch profile and JWT token from backend
189- handleSSOCallback ( ) ;
189+ if ( tokenParam ) {
190+ completeLoginFromToken ( tokenParam ) ;
191+ } else {
192+ // Fallback to session-based profile fetch (useful for local dev)
193+ handleSSOCallback ( ) ;
194+ }
190195 }
191196 // eslint-disable-next-line react-hooks/exhaustive-deps
192197 } , [ searchParams ] ) ;
@@ -248,6 +253,7 @@ const AuthManager = () => {
248253 deleteCookie ( 'jwt' ) ;
249254 setAuthToken ( '' ) ;
250255 setSignedIn ( false ) ;
256+ setRefreshUser ( ( ) => ( ) => { } ) ;
251257 window . location . href = `${ process . env . REACT_APP_SERVER_URL } /api/sso/logout` ;
252258 }
253259
0 commit comments