1

I am creating a xamarin app that uses couchbase lite , sync gateway and couchbase server ,

I followed this tutorial : https://docs.couchbase.com/userprofile-couchbase-mobile/sync/userprofile/xamarin/userprofile_sync.html (but I used server : 127.0.0.1 instead of walrus )

here it is creating users from the config file of sync gateway to be able to sync the user and its data on the server , but I need to create the user from the application itself not from the config file , how can I do this please ?

here is the config file where I create the users :

    {
     "log": ["*","Debug"],
     "databases": {
     "userprofile": {
     "server": "http://127.0.0.1:8091",
     "bucket": "userprofile",
     "username": "Maria", 
     "password": "123456", 
     "enable_shared_bucket_access": true, 
     "import_docs": "continuous",
     "num_index_replicas": 0, 
     "delta_sync" :{"enabled":true},
      "users": {
      "Maria": { "password": "123456"},
      "Mina": { "password": "123456"},
      "GUEST": { "disabled": false, "admin_channels": ["*"] }
  },
      "sync": `
      function sync(doc, oldDoc) {

     /* Authorization */

     // Verify the user making the request is the same as the one in doc's email
     requireUser(doc.email);
     /* Data Validation */

  if (!isDelete()) {
  // Validate the presence of email fields
  validateNotEmpty("email", doc.email);

  // Check if document is being created / added for first time
  // We allow any user to create the document
  if (isCreate()) {

    // Validate that the document Id _id is prefixed by owner.
    var expectedDocId = "user" + "::" + doc.email;

    if (expectedDocId != doc._id) {
        throw({forbidden: "user doc Id must be of form user:email"});

    }

  } else {       

     // Validate that the email hasn't changed.
    validateReadOnly("email", doc.email, oldDoc.email);
  }

}


 /* Routing */
 // Subsequent updates to document must be authorized
 var email = getEmail();

 // Add doc to the user's channel.
 channel("channel." + email);

 /* Access Control */
 // Give user read access to channel
 if (!isDelete()) {
 // Deletion of user document is essentially deletion of user
   access(email,"channel." + email)
 }

 // get type property 
 function getType() {
 return (isDelete() ? oldDoc.type : doc.type);
 }

 // get email Id property
function getEmail() {
return (isDelete() ? oldDoc.email : doc.email);
 }

 // Check if document is being created/added for first time
 function isCreate() {
 // Checking false for the Admin UI to work
  return ((oldDoc == false) || (oldDoc == null || oldDoc._deleted) && !isDelete());
 }

 // Check if this is a document update
 function isUpdate() {
  return (!isCreate() && !isDelete());
}

// Check if this is a document delete
 function isDelete() {
  return (doc._deleted == true);
}

// Verify that specified property exists
function validateNotEmpty(key, value) {
  if (!value) {
    throw({forbidden: key + " is not provided."});
  }
}

// Verify that specified property value has not changed during update
function validateReadOnly(name, value, oldValue) {
  if (value != oldValue) {
    throw({forbidden: name + " is read-only."});
    }
  }
 }
   `
  }
 }
}
Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
Maria Nabil
  • 139
  • 1
  • 10

1 Answers1

0

In the example above you have to create a bucket called userprofile username ** Maria** and password 123456 . Here is the documentation on how to do that:

https://docs.couchbase.com/server/6.0/manage/manage-security/manage-users-and-roles.html

OBS: the user don't need to be called Maria, this is simply the user that Sync gateway will use to connect to couchbase:

 "bucket": "userprofile",
 "username": "someuser", 
 "password": "somepassword"

If you have created a user called someuser with password somepassword , the snippet above is what you have to specify in the sync gateway config file

deniswsrosa
  • 2,421
  • 1
  • 17
  • 25