File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ import { SYSTEM_ROLES } from '@tianji/shared';
2121import { IncomingMessage } from 'http' ;
2222import { type Session } from '@auth/express' ;
2323import { compact , set } from 'lodash-es' ;
24+ import { logger } from '../utils/logger.js' ;
2425
2526export interface UserAuthPayload {
2627 id : string ;
@@ -151,16 +152,30 @@ export async function getAuthSession(
151152 authConfig . basePath
152153 ) ;
153154
155+ if ( ! req . headers . cookie ) {
156+ logger . warn ( 'No cookie in request, can not get auth session' ) ;
157+ }
158+
154159 const response = await Auth (
155160 new Request ( url , { headers : { cookie : req . headers . cookie ?? '' } } ) ,
156161 authConfig
157162 ) ;
158163
159164 const { status = 200 } = response ;
160165
161- const data = await response . json ( ) ;
166+ // Read text first to avoid "Body has already been read" error
167+ const raw = await response . text ( ) ;
168+
169+ let data ;
170+ try {
171+ data = JSON . parse ( raw ) ;
172+ } catch ( error ) {
173+ logger . error ( 'Failed to parse auth session response:' , error ) ;
174+ return null ;
175+ }
162176
163177 if ( ! data || ! Object . keys ( data ) . length ) {
178+ logger . error ( 'Can not get info, auth session raw:' , raw ) ;
164179 return null ;
165180 }
166181
You can’t perform that action at this time.
0 commit comments