Skip to content

Commit 8e74784

Browse files
committed
debug messages for fixing auth issue with transition function
1 parent 1a1295b commit 8e74784

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

nodebook-base/server.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,36 @@ const DISABLE_AUTH = process.env.DISABLE_AUTH === 'true';
2626
const auth = {
2727
async verifyToken(token) {
2828
try {
29+
console.log(`[auth.verifyToken] Verifying token with Keycloak`);
30+
console.log(`[auth.verifyToken] KEYCLOAK_URL: ${KEYCLOAK_URL}`);
31+
console.log(`[auth.verifyToken] KEYCLOAK_REALM: ${KEYCLOAK_REALM}`);
32+
33+
const keycloakUrl = `${KEYCLOAK_URL}/realms/${KEYCLOAK_REALM}/protocol/openid-connect/userinfo`;
34+
console.log(`[auth.verifyToken] Requesting: ${keycloakUrl}`);
35+
2936
// Verify JWT token with Keycloak
30-
const response = await fetch(`${KEYCLOAK_URL}/realms/${KEYCLOAK_REALM}/protocol/openid-connect/userinfo`, {
37+
const response = await fetch(keycloakUrl, {
3138
headers: {
3239
'Authorization': `Bearer ${token}`,
3340
'Content-Type': 'application/json'
3441
}
3542
});
3643

44+
console.log(`[auth.verifyToken] Keycloak response status: ${response.status} ${response.statusText}`);
45+
3746
if (!response.ok) {
47+
const errorText = await response.text();
3848
console.warn(`Keycloak token verification failed: ${response.status} ${response.statusText}`);
49+
console.warn(`Keycloak error response: ${errorText}`);
3950
return null;
4051
}
4152

4253
const userInfo = await response.json();
54+
console.log(`[auth.verifyToken] User info received:`, {
55+
sub: userInfo.sub,
56+
username: userInfo.preferred_username || userInfo.email,
57+
email: userInfo.email
58+
});
4359

4460
return {
4561
id: userInfo.sub,
@@ -105,6 +121,9 @@ fastify.register(import('@fastify/multipart'), {
105121

106122
// Custom authentication hook
107123
async function authenticateJWT(request, reply) {
124+
console.log(`[authenticateJWT] DISABLE_AUTH: ${DISABLE_AUTH}`);
125+
console.log(`[authenticateJWT] Authorization header: ${request.headers.authorization ? 'Present' : 'Missing'}`);
126+
108127
if (DISABLE_AUTH) {
109128
request.user = {
110129
id: 'dev-user-id',
@@ -116,21 +135,27 @@ fastify.register(import('@fastify/multipart'), {
116135
}
117136
const authHeader = request.headers.authorization;
118137
if (!authHeader || !authHeader.startsWith('Bearer ')) {
138+
console.log(`[authenticateJWT] No valid Bearer token found`);
119139
reply.code(401).send({ error: 'No token provided' });
120140
return reply;
121141
}
122142

123143
const token = authHeader.substring(7);
144+
console.log(`[authenticateJWT] Token length: ${token.length}`);
124145
try {
125146
// Verify token with Keycloak
126147
const user = await auth.verifyToken(token);
148+
console.log(`[authenticateJWT] User verification result:`, user ? 'Success' : 'Failed');
127149
if (!user || !user.username) {
150+
console.log(`[authenticateJWT] Invalid user or missing username`);
128151
reply.code(401).send({ error: 'Invalid token' });
129152
return reply;
130153
}
131154

132155
request.user = user;
156+
console.log(`[authenticateJWT] User authenticated: ${user.username} (${user.id})`);
133157
} catch (error) {
158+
console.error(`[authenticateJWT] Token verification error:`, error);
134159
reply.code(401).send({ error: 'Invalid token' });
135160
return reply;
136161
}
@@ -2487,10 +2512,18 @@ Another service or function
24872512
const { graphId, nodeId } = request.params;
24882513
const { morphId } = request.body;
24892514

2515+
console.log(`[POST /api/graphs/${graphId}/nodes/${nodeId}/morph] Request received`);
2516+
console.log(`[POST /api/graphs/${graphId}/nodes/${nodeId}/morph] DISABLE_AUTH: ${DISABLE_AUTH}`);
2517+
console.log(`[POST /api/graphs/${graphId}/nodes/${nodeId}/morph] request.user:`, request.user);
2518+
console.log(`[POST /api/graphs/${graphId}/nodes/${nodeId}/morph] morphId: ${morphId}`);
2519+
24902520
// In dev mode, use a default user ID; otherwise use authenticated user
24912521
const userId = DISABLE_AUTH ? 'dev-user-id' : request.user?.sub;
24922522

2523+
console.log(`[POST /api/graphs/${graphId}/nodes/${nodeId}/morph] userId: ${userId}`);
2524+
24932525
if (!userId) {
2526+
console.log(`[POST /api/graphs/${graphId}/nodes/${nodeId}/morph] No userId found, sending 401`);
24942527
reply.code(401).send({ error: 'Authentication required' });
24952528
return;
24962529
}

0 commit comments

Comments
 (0)