angel-dart / angel

[ARCHIVED] A polished, production-ready backend framework in Dart for the VM, AOT, and Flutter.
https://angel-dart.dev/
MIT License
1.06k stars 67 forks source link

group middleware not working in angel_framework 2.1.1 #277

Closed insinfo closed 3 years ago

insinfo commented 4 years ago

group middleware not working in angel_framework 2.1.1 middleware is not being called

  app.group(
      AppConfig.basePath, (Router router) {
        router.get('/notifications/latest', NotificationController.getLatest);
      },
      middleware: [
        (RequestContext req, ResponseContext res) async {
          res.json({'message': 'access not authorised'});
          return false;
        }
      ],
    );
thosakwe commented 4 years ago

Use app.chain().group(...)

insinfo commented 4 years ago

Thank you for indicating a solution. I was going to suggest modifying line 164 of the router.dart file with that.

/// Creates a route, and allows you to add child routes to it
  /// via a [Router] instance.
  ///
  /// Returns the created route.
  /// You can also register middleware within the router.
  SymlinkRoute<T> group(String path, void Function(Router<T> router) callback, {Iterable<T> middleware, String name}) {
    //from this
    // middleware ??= <T>[];
    // final router = Router<T>().._middleware.addAll(middleware);
    //Isaque changed it here
    //to this
    final router = Router<T>();
    callback(router);
    if (middleware != null) {
      router._routes.forEach((element) {
        element.handlers.insertAll(0, middleware);
      });
    }
    return mount(path, router)..name = name;
  }

I will follow up with your solution