Remote Authentication
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) {// Remote authentication API addressconst checkAuthUrl = 'https://www.example.com/';// Initiate remote authenticationconst checkAuthRes = await fetch(checkAuthUrl);// Authentication passed, normal access to resourcesif (checkAuthRes.status === 200) {return fetch(request, {headers: request.headers,});}// Authentication failed, prohibit access to resourcesreturn 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.