Set Custom Claim in callback function doesn't work

I’m migrating from Rules to Actions and when and the following doesn’t work for me -

var getDb = require('mongo-getdb');
exports.onExecutePostLogin = async (event, api) => {
var namespace = '';
getDb('myconnectionstring', function (db) {
api.idToken.setCustomClaim(namespace + 'appId',"12345678");

when I put the setCustomClaim outside of the getDB callback I do get the claim in the token, but when the setCustomClaim is inside there is no claim in the token.
any ideas?

Hey there @shacharl1 !

I think it’s a matter of function nesting and scopes (in Java Script meaning).

Any particular reason you need to do it that way?

Happy to search for the solution to your problem - just please let us know what you want to achieve with this Action script.

I need to set the custom claim based on the response I’m getting from the getDb, the response is in the callback, is that possible?

Hi @shacharl1 !

Actions are quite flexible but don’t work 1:1 as the Rules feature.

In case you would want to add custom claims if the user logs in in the context of your legacy database, or to a specific app (login context data) then you can use the event object and the properties associated: event.connection and event.client, respectively. More on that here.

Later, based on that context data, you can make further login decisions / add custom claims with the api object.

For a generic detailed design, please see this github repo. In case, more guidance is needed, Auth0 Professional Services 39 can definitely assist in designing/implementing an architecture, aligning to security best practices.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.