fix(cms): correct directus login signature and improve health check diagnostics

This commit is contained in:
2026-02-10 23:35:01 +01:00
parent b5a9083f13
commit 2c88c5dc2e

View File

@@ -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 { config } from './config';
import { getServerAppServices } from './services/create-services.server'; import { getServerAppServices } from './services/create-services.server';
@@ -35,12 +42,12 @@ let authPromise: Promise<void> | null = null;
export async function ensureAuthenticated() { export async function ensureAuthenticated() {
if (token) { if (token) {
(client as any).setToken(token); client.setToken(token);
return; return;
} }
// Check if we already have a valid session token in memory (for login flow) // 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) { if (existingToken) {
return; return;
} }
@@ -52,6 +59,7 @@ export async function ensureAuthenticated() {
authPromise = (async () => { authPromise = (async () => {
try { try {
// Reset current token to ensure fresh login
client.setToken(null as any); client.setToken(null as any);
await client.login(adminEmail, password); await client.login(adminEmail, password);
console.log(`✅ Directus: Authenticated successfully as ${adminEmail}`); console.log(`✅ Directus: Authenticated successfully as ${adminEmail}`);
@@ -164,14 +172,16 @@ export async function checkHealth() {
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
getServerAppServices().errors.captureException(e, { part: 'directus_health_auth' }); 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 { return {
status: 'error', status: 'error',
message: shouldShowDevErrors message: shouldShowDevErrors
? 'Authentication failed. Check your DIRECTUS_ADMIN_EMAIL and DIRECTUS_ADMIN_PASSWORD.' ? `Directus Health Error: ${e.message || 'Unknown'}`
: 'CMS is currently unavailable due to an internal authentication error.', : 'CMS is currently unavailable due to an internal authentication or connection error.',
code: 'AUTH_FAILED', code: e.code || 'HEALTH_AUTH_FAILED',
details: shouldShowDevErrors ? e.message : undefined, details: shouldShowDevErrors
? { message: e.message, code: e.code, errors: e.errors }
: undefined,
}; };
} }