refactor: streamline env and directus logic using @mintel/next-utils and fix network isolation
Some checks failed
Some checks failed
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user