HonoH
Hono•2y ago
Rubyae

Combine JWT middleware with other middleware

I have a file made /middleware/jwt.ts:
import { Context, Next } from 'hono';
import { jwt } from 'hono/jwt';

export default function(c: Context<any, string, {}>, next: Next) {
    const jwtMiddleware = jwt({
        secret: process.env.JWT_SECRET!
    });

    return jwtMiddleware(c, next);
}


Now I'm trying to make a different middleware that checks if the user has certain roles but for this I require the jwtPayload provided by the above middleware

now I can do the following:
app.post('/', jwt, requireRoles([UserRole.Moderator, UserRole.Manager]), async (c) => {});

but it's kind of obvious that if you need to use requireRoles, jwt is also used so I'd like to use the jwt middleware inside the requireRoles middleware. Is this not possible or how should I do this? Because with everything I try I can't seem to make it work 🤔
Was this page helpful?