I want to validate a JWT token in another worker named ‘login’. If I am using hono for the calling worker and the login worker, what is the proper syntax to make the call? The app.post directly to the login worker works fine. I just need to know how to call it from another worker. Thanks for the guidance.
Here is my POST to validate a token inside the login worker. It returns the decrypted payload:
// =========================================
app.post('/validate', async c => {
const token = c.req.header('bearer')
try {
// NOTE: Validate also checks the timestamp TTL
const isValid = await jwt.verify(token, c.env.JWT_TOKEN_KEY, { algorithm: 'HS256' })
if (!isValid) {
return c.json('Invalid signature.');
}
else {
const { payload } = jwt.decode(token)
return c.json(payload)
}
} catch (e) {
return c.json('Invalid signature.');
}
});
// Service Worker
app.fire()
And here is the calling worker’s method:
app.get('/api/spaces', async c => {
const tokenPayload = await c.env.AUTH();
return c.json(tokenPayload);
});
wrangler.toml of calling worker:
services = [
{ binding = "AUTH", service = "login", environment = "production" }
]
Note, I tried this doc but I was not able to figure how to make it work with hono. About Service bindings · Cloudflare Workers docs
Thanks again for the help.
1 Like
I believe the issue is that your URL paths are different as the request goes to /api/spaces
but when it is passed to the second worker there is no path matching /api/spaces
just one matching /validate
. If you look at the examples on the page, then they do not have routing with hono or another library.
1 Like
Thanks for the help. I changed my AUTH worker to look similar to the documentation without hono to be a function, but I still get an error.
AUTH Worker (no other code than this):
export default {
async fetch(request, env) {
return new Response('Request allowed', { status: 200 });
},
};
Calling Worker:
// uses hono routing for this API framework
app.get('/api/spaces', async c => {
const authResponse = await c.env.AUTH.fetch(c.req.clone());
return c.json(authResponse);
});
Error which is thrown in the c.env.AUTH.fetch call above in calling worker while in the Quick Edit:
TypeError: Cannot read properties of undefined (reading 'fetch')
Update:
I just tested the AUTH worker directly and it works as expected, but not the calling worker.
Update #2:
The calling worker is working as expected when called from a URL directly in a browser. If I click the ‘Send’ button in the Quick Edit developer tool, I get the 500 Internal Server Error. So I believe this may be an bug with the developer Quick Edit tool?
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'fetch')
at Object.fetch (worker.js:4)
fetch @ worker.js:4
The quick editor tool does have some missing and broken stuff and shouldn’t be relied on. The TypeError you are seeing is because it doesn’t bind the service worker.
@Cyb3r-Jak3 : Awesome, thanks for the validation.
@Cloudflare: It would have helped if the documentation stated that 