🚀 5 minutes tutorial
Protected Routes

Protected Routes

Protected routes are simple. You just need to add the auth middleware to your routes.

dashboard/dashboard_router.ts
router.group(() => {
  router.get("/general", [DashboardController, "index"]).as("dashboard.index");
  router.get("/account", [DashboardController, "account"]).as("dashboard.account");
  router.get("/admin", [DashboardController, "admin"]).as("dashboard.admin").use([middleware.admin()]);
  router.get("/products", [DashboardController, "products"]).as("dashboard.products");
}).prefix("/dashboard").use([middleware.auth()]);

.use([middleware.auth()]) ensure that the user is authenticated before accessing the route. .use([middleware.admin()]) ensure that the user is an admin before accessing the route.

If you want to protect a route and make it only accessible for admins, Auth middleware first, then Admin middleware.

router.get("/mySuperAdminRouteNotGrouped", [DashboardController, "admin"]).as("dashboard.products").use([middleware.auth(), middleware.admin()]);