Search code examples
dartaqueduct

Add additional properties to response


Using addResponseModifier is it possible to fetch user property and add it to the response body, before the final response? https://pub.dev/documentation/aqueduct/latest/aqueduct/Request/addResponseModifier.html

      .route("/auth/token")
        .linkFunction((req) async {
      req.addResponseModifier((resp) async {
        final q = Query<User>(context)
        ..where((o) => o.username).equalTo(req.body.as()['username'][0] as String);
         final u = await q.fetchOne();
        resp.body = {
          "role": u.role
        };
      });
      return req;
    })
   .link(() => AuthController(authServer));

Solution

  • It turned out that, the method req.addResponseModifier is type vod and my async call for getting the user is executed later on. Thus, using different endpoint.