# CloudFlare Worker verify access token

**URL:** <https://community.auth0.com/t/cloudflare-worker-verify-access-token/82747>\
**Category:** Get Help\
**Created:** [May 2, 2022, 2:46pm UTC](https://community.auth0.com/t/cloudflare-worker-verify-access-token/82747 "2022-05-02T14:46:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MeloHenrique](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/melohenrique/32/36835_2.png) [@MeloHenrique](https://community.auth0.com/u/MeloHenrique)\
**Post date:** [May 2, 2022, 2:46pm UTC](https://community.auth0.com/t/cloudflare-worker-verify-access-token/82747/1 "2022-05-02T14:46:30Z")

</div>

Hi everyone,

I am using auth0 for the first time. I am developing the front end with Blazor, and for the APIs, I am using Cloudflare Workers with Javascript.

I was able to configure RBAC on the front end. However, I can’t make it work on the API.

I am getting the access token from the front end, but every library that I tried to use to verify the token didn’t work.

Sorry if it’s very easy but every tutorial and documentation that I saw was using javascript express.

This is the code that I have:

index.ts

```auto
const PROXY_ENDPOINT = '/api/something/';

addEventListener('fetch', (event) => {

  const request = event.request;
  const url = new URL(request.url);
  const path = url.pathname.replace(/[/]$/, '');
    if (request.method === 'OPTIONS') {
      event.respondWith(handleOptions(request));
    } else if (request.method === 'GET' || request.method === 'PATCH' || request.method === 'POST' || request.method === 'DELETE') {
      event.respondWith(handleRequest(request));
    } else {
      event.respondWith(
          new Response(null, {
            status: 405,
            statusText: 'Method Not Allowed',
          })
      );
    }
});

```

handlerOptions.ts

```auto
export async function handleOptions(request: Request): Promise<Response> {
    let headers = request.headers;
    if (
        headers.get('Origin') !== null &&
        headers.get('Access-Control-Request-Method') !== null &&
        headers.get('Access-Control-Request-Headers') !== null
    ) {
        return new Response(null, {
            headers: {
                'Access-Control-Allow-Origin': '*',
                'Access-Control-Allow-Methods': 'GET, POST, PATCH, DELETE, OPTIONS',
                'Access-Control-Max-Age': '86400',
                'Access-Control-Allow-Headers': request.headers.get('Access-Control-Request-Headers') ?? "",
            },
        });
    } else {
        // Handle standard OPTIONS request.
        // If you want to allow other HTTP Methods, you can do that here.
        return new Response(null, {
            headers: {
                Allow: 'GET, POST, PATCH, DELETE, OPTIONS',
            },
        });
    }
}

```

handler.ts

```auto
export async function handleRequest(request: Request): Promise<Response> {
  const url = new URL(request.url);
  const method = request.method;

  const token = request.headers.get('authorization');
  if (!token) return new Response('Unauthorized', { status: 401 })

  // Verify the access token and get the user permissions to validate 
  // if the user can make the action

  const collection = "collection";

  try{
    if(method === "PATCH"){
      // Do something
    }
    if(method === "POST"){
      // Do something
    }
    if(method === "DELETE"){
      // Do something
    }
    if(method === "GET"){
      // Do something
    }
    return new Response('Not Allowed', { status: 405 });

  } catch (error) {
    return new Response(error.message, { status: 500 });
  }
}

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [May 4, 2022, 5:58pm UTC](https://community.auth0.com/t/cloudflare-worker-verify-access-token/82747/3 "2022-05-04T17:58:02Z")

</div>

Hi @MeloHenrique,

I would recommend using this library to validate tokens:

> **[GitHub - auth0/node-jsonwebtoken: JsonWebToken implementation for node.js...](https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback)**
>
> JsonWebToken implementation for node.js http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html - GitHub - auth0/node-jsonwebtoken: JsonWebToken implementation for node.js http://self-iss...

This section of the readme has plenty of code snippets for different scenarios:

> **[GitHub - auth0/node-jsonwebtoken: JsonWebToken implementation for node.js...](https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback)**
>
> JsonWebToken implementation for node.js http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html - GitHub - auth0/node-jsonwebtoken: JsonWebToken implementation for node.js http://self-iss...

Let me know if you have specific questions.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [June 28, 2022, 10:31pm UTC](https://community.auth0.com/t/cloudflare-worker-verify-access-token/82747/4 "2022-06-28T22:31:20Z")

</div>


