middleware static method

Future middleware(
  1. HttpRequest request,
  2. Future<void> next()
)

Middleware that protects routes requiring authentication.

This middleware:

  • verifies the session cookie exists
  • ensures an in-memory/db session is available
  • checks that the session matches the cookie
  • validates the session timeout window
  • refreshes the last-activity timestamp before continuing

When validation fails, the request is logged out and redirected to the login flow.

Parameters:

  • request: The incoming HTTP request.
  • next: The next middleware or handler in the pipeline.

Example:

router.get(
  '/dashboard',
  handler: (request) async => dashboardController.index(request),
  middleware: [Auth.middleware],
);

Implementation

static Future<dynamic> middleware(HttpRequest request, Future<void> Function() next) async {
  if (await check(request)) {
    return await next();
  }

  return request.redirectToLogin();
}