Skip to content

Why does not a request pass through Middleware ? #3487

Closed Answered by safwanyp
sabotenpillow asked this question in Q&A
Discussion options

You must be logged in to vote

This is an issue with the ordering of your middlewares and handlers.
Middlewares and handlers are executed in an "onion" layout. You can see that documented here and here. Essentially, everything before the await next() in your middleware will be executed before the handler, and everything after the await next() will be executed after the handler.

In the sample code you provided, the middleware is registered after the handler, which means that nothing it is technically never invoked. You can fix it like this:

const checkSession = createMiddleware(async (c, next) => {
  const sessionToken = getCookie(c, "session_token");
  if (sessionToken !== SessionToken) {
    c.status(401);
    return c.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by sabotenpillow
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants