This topic provides sample code that shows how to verify user identity.

Sample code

After EdgeRoutine receives a request from the client, EdgeRoutine reads the AUTH_HEADER_KEY header that is contained in the request. If the value of AUTH_HEADER_KEY is the same as the value of AUTH_HEADER_VALUE, EdgeRoutine returns the requested file. Otherwise, EdgeRoutine rejects the request.

/**
 * A built-in parameter AUTH_HEADER_VALUE in EdgeRoutine is used for identity verification.
 * If the value of AUTH_HEADER_KEY that is contained in the request from the client is different from the value of AUTH_HEADER_VALUE, EdgeRoutine rejects the request.
 */
const AUTH_HEADER_KEY = "AUTH_HEADER_KEY"
const AUTH_HEADER_VALUE = "Value-098372683"

async function handleRequest(request) {
  const value = request.headers.get(AUTH_HEADER_KEY)

  if (value === AUTH_HEADER_VALUE) {
    // If the value of AUTH_HEADER_KEY is the same as the value of AUTH_HEADER_VALUE, identity verification is successful. EdgeRoutine returns the requested content to the client.
    return fetch(request.url)
  }

  // If the value of AUTH_HEADER_KEY is different from the value of AUTH_HEADER_VALUE, EdgeRoutine rejects the request.
  return new Response("403", {
    status: 403,
  })
}

addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})

Results

If the request does not contain AUTH_HEADER_KEY or the value of AUTH_HEADER_KEY is different from the value of AUTH_HEADER_VALUE, the request is rejected.Example 1
If the request contains AUTH_HEADER_KEY and the value of AUTH_HEADER_KEY is the same as the value of AUTH_HEADER_VALUE, the requested content is returned.Example 2