Sync Function - realUserCtx


#1

In sync gateway when using any of the build in function for access like:

requireAccess()
requireRole()
requireUser()

Its uses the realUserCtx object.

Whats the best way to use this object in my custom sync function?

I’ve tried

function(doc,oldDoc,realUserCtx){
console.log(realUserCtx.name);
}

but I’m getting JS process error.


#2

@househippo

realUserCtx is not directly accessible from the sync function, only indirectly available via the require… functions.

When a Sync Gateway _resync is run against a DB all the documents in the DB are run through the sync function again, but there is no real user context during this process. If a user defined sync function was to directly test the value of realUserCtx it would reject the documents on _resync, which is not the desired behaviour.

The require… functions will always evaluate true during a _resync allowing docs to be reprocessed by the Sync Function.


#3
function(doc,oldDoc,realUserCtx){
console.log(realUserCtx.name);
}

Was able to get access to realUserCtx , don’t declare it. This way I can debug.

function(doc,oldDoc){
console.log(realUserCtx.name);
}

hmmmm I also wanted to use the realUserCtx as a supplement to the built-in functions to return a True/False.

On the below functions

requireAccess()
requireRole()
requireUser()

can I catch the values of the failure , throw() or success to be able to form logic that way?


#4

Sync functions are not supposed to be able to access the realUserCtx. I had a look at the source code and it looks like it’s not very securely hidden, which is why your hack works.

Just understand that this is a hack, it’s unsupported, and that realUserCtx will be null in some situations. You can use it for debugging if it helps you, but it should never be used in production.