In this episode, we tackle a common issue: even if a user is signed out, they can still access the dashboard. To fix this, we dive into the concept of middleware pipelines — a way to run several checks ("middleware") before letting someone see certain routes, like the dashboard.
We start by planning how our router can know about which middlewares to use. We decide to add a meta.middleware
array to any route we want to protect, so we can easily stack up multiple middlewares (like checking authentication or if a user has a subscription).
Next, we build a simple middleware as an example and wire it up so our router runs through each middleware for a route, one after another. If someone isn't authenticated, one of these middlewares will redirect them to a login page. We set up a system so that each piece of middleware can call the next one (the pipeline), or stop the process early (like redirecting a user).
Throughout the video, we write and structure the code so it’s reusable: you can easily add as many middleware checks as you want per route, and the pipeline will process them in order. We also clarify some tricky parts, like passing the right context information and building the recursive logic to keep everything flowing smoothly.
By the end, you’ll have a clear system for middleware pipelines in your routing — making it easy to protect any routes in your app and run whatever checks you need before letting users in!