In this example, an Edge Function is used to validate the request headers, implementing basic access control.
const VALID_TOKEN = '123456';
async function handleRequest(request) {
const token = request.headers.get('x-custom-token');
if (token === VALID_TOKEN) {
return new Response('Valid token');
}
return new Response('Invalid token', {
status: 403,
});
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
If authentication fails, access is denied.
If authentication is successful, access is allowed.