Attach data to response if a condition is met

Hello, it is my first post here. I got really stuck.

So, I want to attach some user data to a response when some condition is met. (front end is Angular)

On every incoming request, I trigger ‘beforeRequest’ to check if conditions is met, if so I trigger EVENT_AFTER_PREPARE, but response data and response content are both nonexistent at this moment. (CORS errors are raised for sure)

I can only access data or/and content only after $this->sendHeaders() are executed in Response.php

        if ($updatedAt > $loggedAt) {
             \Yii::$app->response->on(Response::EVENT_AFTER_PREPARE, function ($event) {
            $responseType = \Yii::$app->getResponse()->getHeaders();
            $currentUserData = User::findOne(['id' => 1]);
            \Yii::$app->response->data['username'] = $currentUSerData['username'] //something like this
        });
        }

Would appreciate any help. Thanks

    public function send()
    {
        if ($this->isSent) {
            return;
        }
        $this->trigger(self::EVENT_BEFORE_SEND);
        $this->prepare();
        $this->trigger(self::EVENT_AFTER_PREPARE);
        $this->sendHeaders();
        $this->sendContent();
        $this->trigger(self::EVENT_AFTER_SEND);
        $this->isSent = true;
    } //Response.php