refactor: streamline env and directus logic using @mintel/next-utils and fix network isolation
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 7s
Build & Deploy / 🧪 QA (push) Failing after 1m25s
Build & Deploy / 🏗️ Build (push) Failing after 2m36s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 2s

This commit is contained in:
2026-02-10 23:41:32 +01:00
parent 1fefb794c1
commit 9bcf946752
3 changed files with 46 additions and 167 deletions

View File

@@ -1,18 +1,12 @@
import {
createDirectus,
rest,
authentication,
staticToken,
readItems,
readCollections,
} from '@directus/sdk';
import { readItems, readCollections } from '@directus/sdk';
import { createMintelDirectusClient, ensureDirectusAuthenticated } from '@mintel/next-utils';
import { config } from './config';
import { getServerAppServices } from './services/create-services.server';
const { url, adminEmail, password, token, proxyPath, internalUrl } = config.directus;
const { url, proxyPath, internalUrl } = config.directus;
// Use internal URL if on server to bypass Gatekeeper/Auth
// Use proxy path in browser to stay on the same origin
// Use proxy path in browser to stay on the same origin (CORS safe)
const effectiveUrl =
typeof window === 'undefined'
? internalUrl || url
@@ -20,8 +14,8 @@ const effectiveUrl =
? `${window.location.origin}${proxyPath}`
: proxyPath;
// Initialize client with authentication plugin
const client = createDirectus(effectiveUrl).with(rest()).with(authentication());
// Initialize client using Mintel standards
const client = createMintelDirectusClient(effectiveUrl);
/**
* Helper to determine if we should show detailed errors
@@ -38,49 +32,15 @@ function formatError(error: any) {
return 'A system error occurred. Our team has been notified.';
}
let authPromise: Promise<void> | null = null;
export async function ensureAuthenticated() {
if (token) {
client.setToken(token);
return;
}
// Check if we already have a valid session token in memory (for login flow)
const existingToken = await client.getToken();
if (existingToken) {
return;
}
if (adminEmail && password) {
if (authPromise) {
return authPromise;
try {
await ensureDirectusAuthenticated(client);
} catch (e: any) {
if (typeof window === 'undefined') {
getServerAppServices().errors.captureException(e, { part: 'directus_auth' });
}
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}`);
} catch (e: any) {
if (typeof window === 'undefined') {
getServerAppServices().errors.captureException(e, { part: 'directus_auth' });
}
console.error(`Failed to authenticate with Directus (${adminEmail}):`, e.message);
if (shouldShowDevErrors && e.errors) {
console.error('Directus Auth Details:', JSON.stringify(e.errors, null, 2));
}
// Clear the promise on failure (especially on invalid credentials)
// so we can retry on next request if credentials were updated
authPromise = null;
throw e;
}
})();
return authPromise;
} else if (shouldShowDevErrors && !adminEmail && !password && !token) {
console.warn('Directus: No token or admin credentials provided.');
console.error(`Failed to authenticate with Directus:`, e.message);
throw e;
}
}