Hi, I am going to use sync gateway to synchronise data between iOS devices. Here is my sync config, and in the function requireUser and access I need to get user id as their parameters.
{ "log": ["CRUD", "REST+", "Access"], "facebook": { "register": true }, "databases": { "group": { "server": "walrus:", "users": { "GUEST": {"disabled": true} }, "sync":
function (doc, oldDoc) {
channel(“group”);
requireUser(“1809472385950210”);
access(“1809472385950210”, “group”);
}
} } }
This user id 1809472385950210 can be found in sync gate way log, but how can I get it directly in sync function?
2016-06-26T16:30:07.056+09:00 HTTP: #011: PUT /group/_local/39a4ac10120436a1f6f598bf56c90ef63fd45e3d (as 1809472385950210) 2016-06-26T16:30:51.541+09:00 HTTP: #012: GET /group/_session 2016-06-26T16:30:51.541+09:00 HTTP: #013: GET /group/_session 2016-06-26T16:30:51.545+09:00 HTTP: #014: POST /group/_facebook 2016-06-26T16:30:51.545+09:00 HTTP: #015: POST /group/_facebook 2016-06-26T16:30:51.756+09:00 HTTP: #016: GET /group/_local/2cba18c37eb9d1bbd4d903cac6289d4c557c810a (as 1809472385950210) 2016-06-26T16:30:51.757+09:00 HTTP: #016: --> 404 missing (1.7 ms) 2016-06-26T16:30:51.762+09:00 HTTP: #017: POST /group/_changes (as 1809472385950210) 2016-06-26T16:30:51.767+09:00 HTTP: #018: POST /group/_changes (as 1809472385950210) 2016-06-26T16:30:51.932+09:00 HTTP: #019: GET /group/_local/81dc0c0c1b6f7b70a1ba2fceafd59bd7857b817a (as 1809472385950210) 2016-06-26T16:30:51.932+09:00 HTTP: #019: --> 404 missing (0.2 ms) 2016-06-26T16:30:51.935+09:00 HTTP: #020: POST /group/_revs_diff (as 1809472385950210) 2016-06-26T16:30:51.939+09:00 HTTP: #021: POST /group/_bulk_docs (as 1809472385950210)
Hi, usually you would not hardcode group or user name in the sync function. You would store them in the document. Please take a look at this example: http://developer.couchbase.com/documentation/mobile/current/get-started/get-started-mobile/cloud-sync-function/sync-function-example/index.html
Yes, I do not want to hardcode, it is just a demo. In the function requireUser(“1809472385950210”), I want to get the id which is created by Facebook auth, but how can I can this id? You can see in the log, it appears after all the http request url.
There is no way to retrieve the user id of the active user context in the sync function.
This is by design, during a _resync, all documents are passed through the sync function and at that time there is no authenticated user as the process runs as Admin.
If a sync function were to rely on the user ID being available it would fail for _resync operations.
When a document is created on a client, you would normally have the authenticated userid available, add a property to the document that contains that userid. In your sync function require that the current authenticated user match that doc.userid by calling requireUser(doc.userid)
.
This approach works during a _resync as all the sync function require…() methods will return without error.