javascript BL and authenticated user requests

I read in several topics, that if you want to have Business Logic requests run under logged user, you should use
HeadersManager.getInstance().addHeader( HeadersManager.HeadersEnum.USER_TOKEN_KEY, userToken );
But I understand that’s for Java BL, what about Javascript?

Hi Yuriy,

Try the following:

Backendless.currentUser = {"user-token":userTokenValue};

Please let me know it works.
Mark

I’m new to backendless, so I’m not sure I’m doing all correct. For testing, when object in tableOne is created, I’m creating (in BL) object in tableTwo:

Backendless.ServerCode.Persistence.beforeCreate('tableOne', function(req) {
    Backendless.Logging.setLogReportingPolicy( 1, 1 );
    Backendless.Logging.getLogger('heartbeat').debug("beforeCreate");
    
    var ctx = req.context;
    var item = req.item;
    var userToken = ctx.userToken;
    
    Backendless.currentUser = {"user-token":userToken};
        
    function tableTwo(args) {
        args = args || {};
        this.colText2 = args.colText2 || "";
    }
    
    var tableTwoObject = new tableTwo( {
        colText2: "test"
    });
     
    function objectSaved(savedObject){console.log( "object has been saved" );}
    function gotError(err){
     console.log( "error message - " + err.message );
     console.log( "error code - " + err.statusCode );
    }
    var savedObj = Backendless.Persistence.of( tableTwo ).save( tableTwoObject, new Backendless.Async( objectSaved, gotError ) );
    
});

I expected that with Backendless.currentUser = {“user-token”:userToken}; there will be ownerId for new object, but new object have it empty

This tells me the approach didn’t work. I’ll get one of our JS guys to look into it.

Any updates? I guess I can try calling backendless REST API as workaround?

Hi Yuriy,

Sorry for the delay, have you found a workaround for this?

for proof of concept I tried to use REST API call with user-token header from within BL and looks like it did worked, but I would prefer to be able to use Backendless javascript SDK for that

So, will there be any improvement?

Hi Yuriy,

This functionality has already been implemented, the method signature is the same as for Android. Our JS dev will soon contact you here and provide an exact line of code for this, in case you run into any trouble figuring it out by yourself.

Hi Yuriy,

Sorry for delay. I’ve fixed this problem.
Please, update you Backendless SDK and use next code example for setting user-token in you event handler.
Instead this

Backendless.currentUser = {"user-token":userToken};

use this

Backendless.LocalCache.set('user-token', userToken);

Please, let us know about your progress.

Regards, Ilya

I’ve tested it in debug version, seems working. Thanks!