Add PocketBase token context diagnostics
This commit is contained in:
@@ -57,6 +57,20 @@ async function createValidatedPbClient(token: string): Promise<PocketBase> {
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function decodeJwtPayload(token: string): Record<string, unknown> | null {
|
||||||
|
try {
|
||||||
|
const parts = String(token || '').split('.');
|
||||||
|
if (parts.length < 2) return null;
|
||||||
|
const base = parts[1].replace(/-/g, '+').replace(/_/g, '/');
|
||||||
|
const padded = base + '='.repeat((4 - (base.length % 4 || 4)) % 4);
|
||||||
|
const payload = Buffer.from(padded, 'base64').toString('utf8');
|
||||||
|
const parsed = JSON.parse(payload);
|
||||||
|
return parsed && typeof parsed === 'object' ? parsed as Record<string, unknown> : null;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Shared helpers
|
// Shared helpers
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -447,10 +461,21 @@ app.get('/api/tasks/users', async (c) => {
|
|||||||
if (!pbToken) return c.json({ success: false, message: 'Missing x-pb-token' }, 401);
|
if (!pbToken) return c.json({ success: false, message: 'Missing x-pb-token' }, 401);
|
||||||
|
|
||||||
let taskPb: PocketBase;
|
let taskPb: PocketBase;
|
||||||
|
const tokenPayload = decodeJwtPayload(pbToken);
|
||||||
try {
|
try {
|
||||||
taskPb = await createValidatedPbClient(pbToken);
|
taskPb = await createValidatedPbClient(pbToken);
|
||||||
} catch {
|
} catch (error) {
|
||||||
return c.json({ success: false, message: 'Invalid PocketBase token' }, 401);
|
const message = (error as Error)?.message || String(error);
|
||||||
|
return c.json({
|
||||||
|
success: false,
|
||||||
|
message: 'Invalid PocketBase token',
|
||||||
|
details: message,
|
||||||
|
tokenContext: {
|
||||||
|
type: String(tokenPayload?.type || ''),
|
||||||
|
collectionId: String(tokenPayload?.collectionId || ''),
|
||||||
|
collectionName: String(tokenPayload?.collectionName || ''),
|
||||||
|
},
|
||||||
|
}, 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -505,10 +530,21 @@ app.get('/api/tasks/list', async (c) => {
|
|||||||
if (!pbToken) return c.json({ success: false, message: 'Missing x-pb-token' }, 401);
|
if (!pbToken) return c.json({ success: false, message: 'Missing x-pb-token' }, 401);
|
||||||
|
|
||||||
let taskPb: PocketBase;
|
let taskPb: PocketBase;
|
||||||
|
const tokenPayload = decodeJwtPayload(pbToken);
|
||||||
try {
|
try {
|
||||||
taskPb = await createValidatedPbClient(pbToken);
|
taskPb = await createValidatedPbClient(pbToken);
|
||||||
} catch {
|
} catch (error) {
|
||||||
return c.json({ success: false, message: 'Invalid PocketBase token' }, 401);
|
const message = (error as Error)?.message || String(error);
|
||||||
|
return c.json({
|
||||||
|
success: false,
|
||||||
|
message: 'Invalid PocketBase token',
|
||||||
|
details: message,
|
||||||
|
tokenContext: {
|
||||||
|
type: String(tokenPayload?.type || ''),
|
||||||
|
collectionId: String(tokenPayload?.collectionId || ''),
|
||||||
|
collectionName: String(tokenPayload?.collectionName || ''),
|
||||||
|
},
|
||||||
|
}, 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -710,6 +746,11 @@ app.get('/api/tasks/list', async (c) => {
|
|||||||
email: meRecord?.email || '',
|
email: meRecord?.email || '',
|
||||||
name: meName,
|
name: meName,
|
||||||
},
|
},
|
||||||
|
tokenContext: {
|
||||||
|
type: String(tokenPayload?.type || ''),
|
||||||
|
collectionId: String(tokenPayload?.collectionId || ''),
|
||||||
|
collectionName: String(tokenPayload?.collectionName || ''),
|
||||||
|
},
|
||||||
count: records.length,
|
count: records.length,
|
||||||
lookupMethod,
|
lookupMethod,
|
||||||
warning: warning || undefined,
|
warning: warning || undefined,
|
||||||
|
|||||||
+14
-2
@@ -311,7 +311,13 @@
|
|||||||
});
|
});
|
||||||
const data = await resp.json().catch(() => ({}));
|
const data = await resp.json().catch(() => ({}));
|
||||||
if (!resp.ok || !data?.success) {
|
if (!resp.ok || !data?.success) {
|
||||||
throw new Error(data?.message || 'Failed to load users');
|
const details = data?.details
|
||||||
|
? (typeof data.details === 'string' ? data.details : JSON.stringify(data.details))
|
||||||
|
: '';
|
||||||
|
const context = data?.tokenContext ? ` tokenContext=${JSON.stringify(data.tokenContext)}` : '';
|
||||||
|
throw new Error(details
|
||||||
|
? `${data?.message || 'Failed to load users'}: ${details}${context}`
|
||||||
|
: `${data?.message || 'Failed to load users'}${context}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
usersCache = Array.isArray(data.users) ? data.users : [];
|
usersCache = Array.isArray(data.users) ? data.users : [];
|
||||||
@@ -390,7 +396,10 @@
|
|||||||
const details = data?.details
|
const details = data?.details
|
||||||
? (typeof data.details === 'string' ? data.details : JSON.stringify(data.details))
|
? (typeof data.details === 'string' ? data.details : JSON.stringify(data.details))
|
||||||
: '';
|
: '';
|
||||||
throw new Error(details ? `${data?.message || 'Failed to load tasks'}: ${details}` : (data?.message || 'Failed to load tasks'));
|
const context = data?.tokenContext ? ` tokenContext=${JSON.stringify(data.tokenContext)}` : '';
|
||||||
|
throw new Error(details
|
||||||
|
? `${data?.message || 'Failed to load tasks'}: ${details}${context}`
|
||||||
|
: `${data?.message || 'Failed to load tasks'}${context}`);
|
||||||
}
|
}
|
||||||
if (data?.userName && taskUserSelect.value !== data.userName) {
|
if (data?.userName && taskUserSelect.value !== data.userName) {
|
||||||
taskUserSelect.value = data.userName;
|
taskUserSelect.value = data.userName;
|
||||||
@@ -401,6 +410,7 @@
|
|||||||
userName: data?.userName || userName,
|
userName: data?.userName || userName,
|
||||||
userId: data?.userId || undefined,
|
userId: data?.userId || undefined,
|
||||||
authUser: data?.authUser || undefined,
|
authUser: data?.authUser || undefined,
|
||||||
|
tokenContext: data?.tokenContext || undefined,
|
||||||
lookupMethod: data?.lookupMethod || undefined,
|
lookupMethod: data?.lookupMethod || undefined,
|
||||||
warning: data?.warning || undefined,
|
warning: data?.warning || undefined,
|
||||||
tasks: data.tasks || [],
|
tasks: data.tasks || [],
|
||||||
@@ -416,6 +426,8 @@
|
|||||||
try {
|
try {
|
||||||
await client.collection(config.collection).authRefresh();
|
await client.collection(config.collection).authRefresh();
|
||||||
} catch {
|
} catch {
|
||||||
|
client.authStore.clear();
|
||||||
|
writeOutput('Existing PocketBase session could not refresh for Users collection. Please login again.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user