How to access URL fragment (stuff after #) in Worker

Hi, I’m trying to use AWS Cognito to authenticate users of a Cloudflare Workers Site, I’ve got stuck when it comes to extracting the JWT from the URL returned by Cognito after a successful authentication.

If an unauthenticated user tries to access a restricted page in my workers site I do an HTTP redirect to Cognito. If the user authenticates successfully then Cognito does an HTTP redirect back to my Workers Site, the return URL Cognito generates is of the form:

The interesting point about this URL is that the information I’m after is in the URL “fragment” (the stuff after the #).

How can I access the section of the URL after the # within the JavaScript executed by my worker?

I’d assumed it would be available in the hash property of the URL object (created as per the examples using const url = new URL(event.request.url); ) but the value is not set.

If I manually create a URL that changes the # into a ? then the search property of the URL is populated…

I’ve tried running the code locally with wrangler dev and watching a load of console.log statements viewed using wrangler tail (all good learning for me) but the

I’m new to the Workers platform so I may be doing something dim!

Any suggestions gratefully received.

Cheers, Andy

The fragment is something which is only handled on the client side and is never part of the request, hence Workers cannot access it. has some workarounds.

Ahh, of course, you are completely correct - thank you :slight_smile:

For now I’ll take a look at doing something on the client side to get hold of the hash.