Add PocketBase token context diagnostics
This commit is contained in:
@@ -57,6 +57,20 @@ async function createValidatedPbClient(token: string): Promise<PocketBase> {
|
||||
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
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -447,10 +461,21 @@ app.get('/api/tasks/users', async (c) => {
|
||||
if (!pbToken) return c.json({ success: false, message: 'Missing x-pb-token' }, 401);
|
||||
|
||||
let taskPb: PocketBase;
|
||||
const tokenPayload = decodeJwtPayload(pbToken);
|
||||
try {
|
||||
taskPb = await createValidatedPbClient(pbToken);
|
||||
} catch {
|
||||
return c.json({ success: false, message: 'Invalid PocketBase token' }, 401);
|
||||
} catch (error) {
|
||||
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 {
|
||||
@@ -505,10 +530,21 @@ app.get('/api/tasks/list', async (c) => {
|
||||
if (!pbToken) return c.json({ success: false, message: 'Missing x-pb-token' }, 401);
|
||||
|
||||
let taskPb: PocketBase;
|
||||
const tokenPayload = decodeJwtPayload(pbToken);
|
||||
try {
|
||||
taskPb = await createValidatedPbClient(pbToken);
|
||||
} catch {
|
||||
return c.json({ success: false, message: 'Invalid PocketBase token' }, 401);
|
||||
} catch (error) {
|
||||
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 {
|
||||
@@ -710,6 +746,11 @@ app.get('/api/tasks/list', async (c) => {
|
||||
email: meRecord?.email || '',
|
||||
name: meName,
|
||||
},
|
||||
tokenContext: {
|
||||
type: String(tokenPayload?.type || ''),
|
||||
collectionId: String(tokenPayload?.collectionId || ''),
|
||||
collectionName: String(tokenPayload?.collectionName || ''),
|
||||
},
|
||||
count: records.length,
|
||||
lookupMethod,
|
||||
warning: warning || undefined,
|
||||
|
||||
Reference in New Issue
Block a user