-
Notifications
You must be signed in to change notification settings - Fork 345
Using Clerk with Cloudflare Workers #1421
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I too am planning on verifying token in cloudflare worker. I've seen another method used in the discord but it'd be nice to see an example repo for this since it's definitely a bit more work: https://discord.com/channels/856971667393609759/1112517462680944640/1112517462680944640 Also are there ways to store small meta-data in that token such as userId so i can make more use of it in the workers environment? |
If you're using Cloudflare Workers, you should use the @clerk/backend package. This is how I verified requests on cf workers: import {
verifyToken,
} from "@clerk/backend";
export const verifyReqJWT = async (pemString: string, req: Request) => {
const reqJwtToken = req.headers.get("Authorization");
if (!reqJwtToken || reqJwtToken === "undefined" || reqJwtToken === "null") {
return undefined;
}
return await verifyToken(reqJwtToken, {
issuer: "https://<your-identifier>.clerk.accounts.dev",
jwtKey: pemString,
});
}; Where you get the Set the CLERK_PEM="-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----" It is truly bizarre how bad the docs are on this. They recommend using the The implementation of the various functions to get the signed-in context objects etc. are really weird and I spent hours scouring the internet and their code to find out how to do things properly. There's so little on how to go from a PEM string + a JWT token to a verified JwtPayload. In an edge function, you don't want to be making additional requests to the .well-known URL, so you want to the PEM string locally. Anyway, hope that helps. |
Yes, to verify the token and everything I used @tsndr/cloudflare-worker-jwt: const valid = await jwt.verify(token, c.env.JWT_ACCESS_SIGN_KEY, { algorithm: "RS256" }); And the JWT_ACCESS_SIGN_KEY is the PEM in a string. |
Potential overlap: #1541 |
The NPM |
An example of how
|
We can close this issue then. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Package + Version
@clerk/clerk-js
@clerk/clerk-react
@clerk/nextjs
@clerk/remix
@clerk/types
@clerk/themes
@clerk/localizations
@clerk/clerk-expo
@clerk/backend
@clerk/clerk-sdk-node
@clerk/shared
@clerk/fastify
@clerk/chrome-extension
gatsby-plugin-clerk
build/tooling/chore
Dependencies + versions
Browser/OS
Cloudflare worker 3.1.1
Description
I tried to use @clerk/clerk-sdk-node to interact with Clerk from my cloudflare workers, but due to how the environment variables are declared, using directly process.env, I cannot use it in Cloudflare workers, so I had to use the @clerk/backend.
I know that Cloudflare is not node.
I could not find any reference to Cloudflare Workers in the documentation.
There is a thread in Discord: https://discord.com/channels/856971667393609759/1105772611511791666
The text was updated successfully, but these errors were encountered: