tencent cloud

All product documents
Tencent Cloud EdgeOne
Authenticating a Request Header
Last updated: 2024-01-25 11:43:50
Authenticating a Request Header
Last updated: 2024-01-25 11:43:50
This example demonstrates how to use an edge function to perform simple permission control by verifying the value of the x-custom-token request header. If the value is token-123456, access is allowed. Otherwise, access is denied.

Sample Code

async function handleRequest(request) {
const token = request.headers.get('x-custom-token');

if (token === 'token-123456') {
return new Response('hello world');
}

// Incorrect key supplied. Reject the request.
return new Response('Sorry, you have supplied an invalid token.', {
status: 403,
});
}

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

Sample Preview

In the address bar of the browser, enter a URL that matches a trigger rule of the edge function to preview the effect of the sample code.
If authentication fails, access is denied.

If authentication is successful, access is allowed.


References

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon