In order to avoid customers' resources being accessed by illegal users, this example transmits the request to the customer-specified remote authentication server. The authentication server verifies the user's request, and the Edge functions decide whether to allow access to the target resources based on the check result returned by the remote authentication server. If the authentication fails, the client will be responded with a 403 status code.
async function handleRequest(request) {
const checkAuthUrl = 'https://www.example.com/';
const checkAuthRes = await fetch(checkAuthUrl);
if (checkAuthRes.status === 200) {
return fetch(request, {
headers: request.headers,
});
}
return new Response(null, {
status: 403
});
}
addEventListener('fetch', e => {
e.respondWith(handleRequest(e.request));
});
Example preview
Enter the URL that matches the triggering rules of the Edge functions in the address bar of the browser on both PC and mobile (e.g., https://example.com/app/index.html
) to preview the example effect.
Authentication passed, normal access to resources.
Authentication failed, prohibit access to resources.
Related references
Was this page helpful?