@@ -19,12 +19,12 @@ export const Route = createFileRoute('/api/auth/callback/$provider')({
1919 const error = url . searchParams . get ( 'error' )
2020
2121 if ( error ) {
22- console . error ( `[OAuth Callback ] OAuth error received: ${ error } ` )
22+ console . error ( `[AUTH:ERROR ] OAuth error received from provider : ${ error } ` )
2323 return Response . redirect ( new URL ( '/login?error=oauth_failed' , request . url ) , 302 )
2424 }
2525
2626 if ( ! code || ! state ) {
27- console . error ( '[OAuth Callback ] Missing code or state' )
27+ console . error ( '[AUTH:ERROR ] Missing code or state in OAuth callback ' )
2828 return Response . redirect ( new URL ( '/login?error=oauth_failed' , request . url ) , 302 )
2929 }
3030
@@ -35,14 +35,14 @@ export const Route = createFileRoute('/api/auth/callback/$provider')({
3535 . find ( ( c ) => c . trim ( ) . startsWith ( 'oauth_state=' ) )
3636
3737 if ( ! stateCookie ) {
38- console . error ( '[OAuth Callback ] No state cookie found' )
38+ console . error ( '[AUTH:ERROR ] No state cookie found - possible CSRF or cookie issue ' )
3939 return Response . redirect ( new URL ( '/login?error=oauth_failed' , request . url ) , 302 )
4040 }
4141
4242 const cookieState = decodeURIComponent ( stateCookie . split ( '=' ) . slice ( 1 ) . join ( '=' ) . trim ( ) )
4343
4444 if ( cookieState !== state ) {
45- console . error ( '[OAuth Callback ] State mismatch' )
45+ console . error ( `[AUTH:ERROR ] State mismatch - expected: ${ cookieState . substring ( 0 , 10 ) } ..., received: ${ state . substring ( 0 , 10 ) } ...` )
4646 return Response . redirect ( new URL ( '/login?error=oauth_failed' , request . url ) , 302 )
4747 }
4848
@@ -88,9 +88,15 @@ export const Route = createFileRoute('/api/auth/callback/$provider')({
8888
8989 const tokenData = await tokenResponse . json ( )
9090 if ( tokenData . error ) {
91- console . error ( '[OAuth Callback ] GitHub OAuth error received' )
91+ console . error ( `[AUTH:ERROR ] GitHub token exchange failed: ${ tokenData . error } , description: ${ tokenData . error_description || 'none' } ` )
9292 throw new Error ( `GitHub OAuth error: ${ tokenData . error } ` )
9393 }
94+
95+ if ( ! tokenData . access_token ) {
96+ console . error ( '[AUTH:ERROR] GitHub token exchange succeeded but no access_token returned' )
97+ throw new Error ( 'No access token received from GitHub' )
98+ }
99+
94100 accessToken = tokenData . access_token
95101 // Fetch user profile
96102 const profileResponse = await fetch ( 'https://api.github.com/user' , {
@@ -121,7 +127,8 @@ export const Route = createFileRoute('/api/auth/callback/$provider')({
121127 }
122128
123129 if ( ! email ) {
124- throw new Error ( 'No verified email found for GitHub account' )
130+ console . error ( `[AUTH:ERROR] No verified email found for GitHub user ${ profile . id } (${ profile . login } )` )
131+ throw new Error ( 'No verified email found for GitHub account' )
125132 }
126133
127134 userProfile = {
@@ -155,9 +162,15 @@ export const Route = createFileRoute('/api/auth/callback/$provider')({
155162
156163 const tokenData = await tokenResponse . json ( )
157164 if ( tokenData . error ) {
158- console . error ( '[OAuth Callback ] Google OAuth error received' )
165+ console . error ( `[AUTH:ERROR ] Google token exchange failed: ${ tokenData . error } , description: ${ tokenData . error_description || 'none' } ` )
159166 throw new Error ( `Google OAuth error: ${ tokenData . error } ` )
160167 }
168+
169+ if ( ! tokenData . access_token ) {
170+ console . error ( '[AUTH:ERROR] Google token exchange succeeded but no access_token returned' )
171+ throw new Error ( 'No access token received from Google' )
172+ }
173+
161174 accessToken = tokenData . access_token
162175
163176 // Fetch user profile
@@ -173,7 +186,8 @@ export const Route = createFileRoute('/api/auth/callback/$provider')({
173186 const profile = await profileResponse . json ( )
174187
175188 if ( ! profile . verified_email ) {
176- throw new Error ( 'Google email not verified' )
189+ console . error ( `[AUTH:ERROR] Google email not verified for user ${ profile . id } (${ profile . email } )` )
190+ throw new Error ( 'Google email not verified' )
177191 }
178192
179193 userProfile = {
@@ -193,6 +207,7 @@ export const Route = createFileRoute('/api/auth/callback/$provider')({
193207 } )
194208
195209 if ( ! user ) {
210+ console . error ( `[AUTH:ERROR] User ${ result . userId } not found after OAuth account creation for ${ provider } :${ userProfile . id } (${ userProfile . email } )` )
196211 throw new Error ( 'User not found after OAuth account creation' )
197212 }
198213
@@ -224,7 +239,11 @@ export const Route = createFileRoute('/api/auth/callback/$provider')({
224239 headers,
225240 } )
226241 } catch ( err ) {
227- console . error ( '[API OAuth Callback] Error:' , err instanceof Error ? err . message : 'Unknown error' )
242+ console . error ( '[AUTH:ERROR] OAuth callback failed:' , {
243+ error : err instanceof Error ? err . message : 'Unknown error' ,
244+ stack : err instanceof Error ? err . stack : undefined ,
245+ provider : params . provider ,
246+ } )
228247 return Response . redirect ( new URL ( '/login?error=oauth_failed' , request . url ) , 302 )
229248 }
230249 } ,
0 commit comments