How to set realm id and partition with custom login?


I’m using a custom authentication function so that a mobile client generates a unique id and logs in with it.

My function is

exports = async function(loginPayload) {
  const app_secret = "somesecret"
  const users =

  const { clientUserId, secret } = loginPayload;
  if(app_secret !== secret) {
    throw "Secret mismatch : " + secret;

  const user = await users.findOne({ clientUserId: clientUserId });
  if (user) {
    return user._id.toString();
  } else {
    const newId = new BSON.ObjectId();
    const partition = "user=" + newId.toString();
    const result = await users.insertOne({ _id: newId, _partition: partition, clientUserId: clientUserId, creationDate: new Date() });
    return result.insertedId.toString();

My problem is that the “realm user id”, the one that RealmSDK gives me on mobile client is different from the one I generate in the custom function.

And I have set the permissions to

(can only read PUBLIC and your own data such as partitions are "user=myId123" or "PUBLIC")
  "$or": [
      "%%partition": ""
      "%%partition": "PUBLIC"

(can only write your own data such as partitions are "user=myId123")
  "%%partition": ""

How am I supposed to retrieve this realm user id so that I can set up correctly my custom login function and populate correctly my collection with the right _partition and _id?

EDIT: The error I get from Realm Sync Backend is

Error: user does not have permission to sync on partition (ProtocolErrorCode=206)
Partition: user=607ac1b7687c4dca433c3c5e

Because my custom function has set the custom user id to 607ac1b7687c4dca433c3c5b but the actual realm user id is 607ac1b7687c4dca433c3c5e and I have no clue how to fetch this id from the custom login function…