Fix: Add fallback token retrieval endpoint and improved token extraction debugging
This commit is contained in:
@@ -82,6 +82,48 @@ app.get('/version', (c) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Get Graph token from PocketBase auth data
|
||||
app.get('/api/graph-token', async (c) => {
|
||||
try {
|
||||
const authHeader = c.req.header('Authorization');
|
||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||
return c.json({ error: 'Unauthorized' }, 401);
|
||||
}
|
||||
|
||||
const pbToken = authHeader.substring(7);
|
||||
|
||||
// Fetch the authenticated user's data from PocketBase
|
||||
const response = await fetch('https://pocketbase.ccllc.pro/api/collections/users/auth-refresh', {
|
||||
method: 'POST',
|
||||
headers: { 'Authorization': `Bearer ${pbToken}` }
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return c.json({ error: 'Failed to refresh auth' }, response.status);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const meta = data?.meta || {};
|
||||
|
||||
// Extract Graph token from all possible locations
|
||||
const graphToken =
|
||||
meta?.graphAccessToken ||
|
||||
meta?.graph_token ||
|
||||
meta?.graphToken ||
|
||||
meta?.accessToken ||
|
||||
meta?.access_token ||
|
||||
meta?.token ||
|
||||
meta?.rawToken ||
|
||||
meta?.authData?.access_token ||
|
||||
'';
|
||||
|
||||
return c.json({ token: graphToken });
|
||||
} catch (err) {
|
||||
logLine('logs/error.log', `graph-token endpoint error: ${(err as Error)?.message || String(err)}`);
|
||||
return c.json({ error: 'Failed to get token', token: '' }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
// Jobs endpoint with Redis caching
|
||||
app.get('/api/jobs', async (c) => {
|
||||
const page = parseInt(c.req.query('page') || '1');
|
||||
|
||||
Reference in New Issue
Block a user