cancel
Showing results for 
Search instead for 
Did you mean: 

CAP event handler authentication data

JuanjoGersol
Explorer
0 Kudos

Dear Experts,

I have a problem on an app I'm working on, maybe you can give me some hints.

I have a service that manages a document with a status. The document status can be changed from different places/processes. When a change is done to the status I have to write information into another service that stores every document status change for tracking purposes.

Both services are exposed though the app router with XSUAA authentication and they work as expected in most of the cases.


I have some event handlers like this:

srv.after('UPDATE','Documents', async(req, data) {
    const ht = await cds.connect.to('History');
    const bearer = req.headers.authorization || '';

    ht.before('*', (request) => {
      request.headers.authorization = bearer;
    });
    payload = { status: .... }
    ht.AddHistory(JSON.stringify(payload));
}

srv.on('Function', async(req) {
    ...
    await this.update('Documents', key).with({status});
}

When I trigger an update of the status from the UI (fiori) the after event contains a valid req.user user information and also the bearer token I need for the authentication of the external service.

When I trigger the update from the Function the req.user information is empty an no bearer is provider. Please note that at the moment of the call both of them are available.

I have tried with this.tx(req).update and other combinations without success.
Can someone clarify why when triggering the update from CAP the event handler for after.update is not launched with the user information?

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

JuanjoGersol
Explorer
0 Kudos

Hello,

I skipped the problem above changing the design of the implementation. But recently I came to a similar problem where from a service handler implementation I had to call an external service and the request came with no headers and no user.

srv.after('UPDATE','Documents', async(req, data) {
    const ht = await cds.connect.to('History');
    const bearer = req.headers.authorization || req.contexts.headers.authorization ||'';

    ht.before('*', (request) => {
      request.headers.authorization = bearer;
    });
    payload = { status: .... }
    ht.AddHistory(JSON.stringify(payload));
}

srv.on('Function', async(req) {
    ...
    await this.update('Documents', key).with({status});
}

Answers (0)