fix(cms): correct directus login signature and improve health check diagnostics
All checks were successful
Build & Deploy / 🔍 Prepare (push) Successful in 6s
Build & Deploy / 🧪 QA (push) Successful in 1m31s
Build & Deploy / 🏗️ Build (push) Successful in 2m58s
Build & Deploy / 🚀 Deploy (push) Successful in 31s
Build & Deploy / 🔔 Notify (push) Successful in 1s

This commit is contained in:
2026-02-10 23:35:01 +01:00
parent 1c1aebb804
commit 1fefb794c1

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 { getServerAppServices } from './services/create-services.server';
@@ -35,12 +42,12 @@ let authPromise: Promise<void> | 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,
};
}