dartdart-shelfdart-servershelf-router

Dart shelf server: only apply middleware to selected routes


In Dart shelf_router, I can add middleware like so:

final handler = Pipeline()
    .addMiddleware(logRequests())
    .addMiddleware(auth())
    .addHandler(router.call);

However, I want the auth middleware to only apply to certain routes, not all of them:

final router = Router()
  ..get('/public', _publicHandler)    // no auth
  ..get('/private', _privateHandler); // this route should apply auth

How would I do that?


Solution

  • Does https://codereview.stackexchange.com/a/274219 help? From what I can see, you could just wrap your own handler in Pipeline().addMiddleware().addHandler(), and that way you could target specific routes?