Skip to content

Commit 5eb57f4

Browse files
committed
refactor: add more log for get auth session which maybe can help to debug
1 parent 9c0838e commit 5eb57f4

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/server/model/auth.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { SYSTEM_ROLES } from '@tianji/shared';
2121
import { IncomingMessage } from 'http';
2222
import { type Session } from '@auth/express';
2323
import { compact, set } from 'lodash-es';
24+
import { logger } from '../utils/logger.js';
2425

2526
export 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

0 commit comments

Comments
 (0)