Never able to authenticate CouchbaseLite replication

Hi

I’m working on a Xamarin .NET app which requires replication. I have an existing server and infrastructure that has been working fine for a good while using the old 1.4 Couchbase Lite and 1.5 sync gateway.

I’ve have updated to the latest couchbase lite and the newest sync gateway.

My server authenticates using the POST against _session and retrieves the session cookie etc.and returns this back to the client ready to be used.

I then try to start replication using the this session id using the following code and it ALWAYS returns:

Couchbase.Lite.CouchbaseLiteException: CouchbaseLiteException (LiteCoreDomain / 26): Authentication failed, see inner exception
(there is nothing in the inner exception).

Which is odd as I have already authenticated successfully using _session.

the URL I am using is of the form:
“wss://xxx.yyyy.zzz:4984/mydatabasename”

The code I am using is as follows:
var targetEndpoint = new URLEndpoint(new Uri(config.CouchbaseGatewayServiceUrl));
var replConfig = new ReplicatorConfiguration(database, targetEndpoint);

			replConfig.ReplicatorType = ReplicatorType.PushAndPull;
			replConfig.Continuous = true;
			replConfig.Authenticator = new SessionAuthenticator(session.CookieValue);

			replicator = new Replicator(replConfig);
			replicator.AddDocumentReplicationListener((object sender, DocumentReplicationEventArgs e) =>
			  {
				  Debug.WriteLine($"Doc Count {e.Documents.Count}");
			  });

			replicator.AddChangeListener((sender, changeArgs) =>
			{
				if (changeArgs.Status.Error != null)
				{
// *************** ALWAYS has an error ***************
					Console.WriteLine($"Error :: {changeArgs.Status.Error}");
				}

				Debug.WriteLine($"Change Id  Status {changeArgs.Status.Activity} Progress {changeArgs.Status.Progress}");
			});
			replicator.Start();

Any thoughts of what I have missed?

Added note:

I also noticed the following lines in the console after I call Start() but before it eventually fails with the auth failure
2019-10-16 13:01:25.801053+0100 QContractor.iOS[33048:4941995] [20]| WARNING) [Network] {C4SocketImpl#1}==> litecore::repl::C4SocketImpl wss://qcdev.qgroup.tech:4984/qcontractor/_blipsync @0x7f8eeee4d6b0
2019-10-16 13:01:25.801501+0100 QContractor.iOS[33048:4941995] [20]| WARNING) [Network] {C4SocketImpl#1} Unexpected or unclean socket disconnect! (reason=Unknown error, code=10)

Check out this blog which describes the flow . You may want to look into the extraction of the session_id within your mobile app and confirm it’s OK.

While this isn’t the issue, something to be considered - are you planning to have everyone who is using the sync gateway to be authenticated as a single user ? I suspect not. In which case , you must create relevant users on the Sync gateway post authentication. Typical pattern is to create user (using /user REST endpoint) after first time authentication if one does not exist. The blog discusses that as well. (BTW you don’t have to store password when you create the user on sync gateway)