Custom jwt middleware
I want to check whether the user exists in the database based on the ID that I took from the sub payload using JWT middleware. Anyone can help me for the example code?
5 Replies
It really depends on the database you are using. Here is an example of an JWT Auth middleware https://github.com/NicoPlyley/hono-auth/blob/main/src/middleware/protect.ts
GitHub
hono-auth/src/middleware/protect.ts at main ยท NicoPlyley/hono-auth
An example app with authentication using Hono, Drizzle, and D1. Running on CF Workers - NicoPlyley/hono-auth
thanks bro, but I mean can we create a middleware that extends with the built-in jwt middleware? So we can use
c.get('jwtPayload')
to get sub payload instead of write the code from line 11 to 26As far as extending it directly I don't think so. But you can write another middleware to just do the database check
You can do something like this to make it into one middleware as well. That way you don't have to define two each time
Why didn't I think of combining them into an array? ๐๐
thanks bro
No problem!