From 1fefb794c1cc8d435d77040db52c4d2c489d7780 Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Tue, 10 Feb 2026 23:35:01 +0100 Subject: [PATCH] fix(cms): correct directus login signature and improve health check diagnostics --- lib/directus.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/directus.ts b/lib/directus.ts index 133e51b9..085c2c8c 100644 --- a/lib/directus.ts +++ b/lib/directus.ts @@ -1,4 +1,11 @@ -import { createDirectus, rest, authentication, staticToken, readItems, readCollections } from '@directus/sdk'; +import { + createDirectus, + rest, + authentication, + staticToken, + readItems, + readCollections, +} from '@directus/sdk'; import { config } from './config'; import { getServerAppServices } from './services/create-services.server'; @@ -35,12 +42,12 @@ let authPromise: Promise | null = null; export async function ensureAuthenticated() { if (token) { - (client as any).setToken(token); + client.setToken(token); return; } // Check if we already have a valid session token in memory (for login flow) - const existingToken = await (client as any).getToken(); + const existingToken = await client.getToken(); if (existingToken) { return; } @@ -52,6 +59,7 @@ export async function ensureAuthenticated() { authPromise = (async () => { try { + // Reset current token to ensure fresh login client.setToken(null as any); await client.login(adminEmail, password); console.log(`✅ Directus: Authenticated successfully as ${adminEmail}`); @@ -63,7 +71,7 @@ export async function ensureAuthenticated() { if (shouldShowDevErrors && e.errors) { console.error('Directus Auth Details:', JSON.stringify(e.errors, null, 2)); } - // Clear the promise on failure (especially on invalid credentials) + // Clear the promise on failure (especially on invalid credentials) // so we can retry on next request if credentials were updated authPromise = null; throw e; @@ -164,14 +172,16 @@ export async function checkHealth() { if (typeof window === 'undefined') { getServerAppServices().errors.captureException(e, { part: 'directus_health_auth' }); } - console.error('Directus authentication failed during health check:', e); + console.error('Directus authentication or collection-read failed during health check:', e); return { status: 'error', message: shouldShowDevErrors - ? 'Authentication failed. Check your DIRECTUS_ADMIN_EMAIL and DIRECTUS_ADMIN_PASSWORD.' - : 'CMS is currently unavailable due to an internal authentication error.', - code: 'AUTH_FAILED', - details: shouldShowDevErrors ? e.message : undefined, + ? `Directus Health Error: ${e.message || 'Unknown'}` + : 'CMS is currently unavailable due to an internal authentication or connection error.', + code: e.code || 'HEALTH_AUTH_FAILED', + details: shouldShowDevErrors + ? { message: e.message, code: e.code, errors: e.errors } + : undefined, }; }