| title | Box UI Elements Demo |
|---|
import { ContentPreview, ContentExplorer } from "/snippets/box-ui-proxy.jsx"
This page demonstrates how Box UI Elements can be embedded securely using the interceptor/proxy pattern to handle authentication server-side.
The proxy approach provides several security and architectural benefits:
- No token exposure: Authentication tokens never reach the client browser
- Server-side control: Full control over API requests and responses
- Custom logic: Add filtering, logging, or custom business logic
- Token management: Automatic token refresh and management
The Box Content Preview UI element allows you to embed a file preview directly in your application.
Here's how you import and use the ContentPreview component with proxy support:
import { ContentPreview } from "/snippets/box-ui-proxy.jsx"
<ContentPreview
fileId="123456789"
useProxy={true}
proxyUrl="/api/box/proxy"
hasHeader={true}
hasDownload={true}
language="en-US"
onLoad={(data) => {
console.log('Preview loaded:', data);
}}
onError={(error) => {
console.error('Preview error:', error);
}}
/>Content Preview with Custom File ID:
Content Preview with Default Demo File:
Content Explorer with Custom Folder:
You can easily embed file previews in your documentation by specifying the file ID:
<!-- Preview a specific PDF -->
<ContentPreview fileId="987654321" />
<!-- Preview with custom settings -->
<ContentPreview
fileId="555666777"
hasHeader={false}
hasDownload={false}
/>
<!-- Explore a specific folder -->
<ContentExplorer folderId="111222333" />To use the proxy approach, you need a backend service that handles Box API authentication. Here's a complete Express.js implementation:
// Example Express.js backend proxy for Box UI Elements
const express = require('express');
const axios = require('axios');
const app = express();
app.use(express.json());
// Box API proxy endpoint
app.post('/api/box/proxy', async (req, res) => {
try {
const { originalUrl, originalMethod, originalData, originalParams } = req.body;
// Get secure access token (implement your auth logic here)
const accessToken = await getSecureAccessToken(req);
// Prepare request to Box API
const boxRequest = {
method: originalMethod.toLowerCase(),
url: originalUrl,
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}
};
if (originalData) boxRequest.data = originalData;
if (originalParams) boxRequest.params = originalParams;
// Make request to Box API
const boxResponse = await axios(boxRequest);
res.json(boxResponse.data);
} catch (error) {
const status = error.response?.status || 500;
const errorData = error.response?.data || { type: 'error', message: 'Proxy error' };
res.status(status).json(errorData);
}
});
async function getSecureAccessToken(req) {
// TODO: Implement your token management logic
// Examples:
// - JWT token generation
// - OAuth2 token retrieval
// - App token usage
// - User session validation
}The proxy implementation supports custom logic through interceptors:
const requestInterceptor = (config) => {
if (config.method === "post" && config.url.startsWith("https://upload")) {
const formData = JSON.parse(config.data.attributes);
if (formData.name.endsWith(".zip")) {
// Redirect to error endpoint
config.url = "https://api.box.com/2.0/files/blocked";
alert("ZIP files are not allowed");
}
}
return config;
};const responseInterceptor = (config) => {
if (config.request.responseURL.includes('item_collection')) {
config.data.item_collection.entries = config.data.item_collection.entries.filter((entry) => {
return !entry.name || !/\.(docx|xlsx|pptx)$/i.test(entry.name);
});
}
return config;
};const responseInterceptor = (config) => {
if (config.data.type === 'file' && config.data.metadata?.global?.MalwareDeepScan) {
// Show malware warning banner
document.getElementById('warning-banner').style.display = 'block';
}
return config;
};- Secure Authentication: Tokens never exposed to client
- Custom Business Logic: Filter, transform, or enhance API responses
- Monitoring & Logging: Track all API interactions server-side
- Access Control: Implement fine-grained permissions
- Token Management: Automatic refresh and downscoping
- Security: No sensitive tokens in browser
- Control: Server-side filtering and validation
- Monitoring: Centralized logging and analytics
- Flexibility: Custom business rules and transformations
- Performance: Caching and optimization opportunities
This proxy pattern is the recommended approach for production Box UI Elements implementations.