Sync gateway authentication check

I have an appthat requires a sign up screen. I am setting up the replication service as follows in .NET:

protected void CreateContinuousReplications(string username, string password, Uri uri)
    var push = db.CreatePushReplication(uri);
    var pull = db.CreatePullReplication(uri);
    var auth = AuthenticatorFactory.CreateBasicAuthenticator(username, password);
    push.Authenticator = auth;
    pull.Authenticator = auth;
    push.Continuous = true;
    pull.Continuous = true;

I have no problems syncing documents. But, how do I check on a code level if the credentials have been authenticated? Currently, if the wrong username/password are entered, I can’t see any way of checking if it connected to sync gateway from code…

If they are not authenticated then the replication will fail. There was an issue with not being able to tell from the exception whether or not it was an auth problem or some other problem but I can’t remember the exact circumstances. You should be monitoring the changes callback for any exceptions and other info.

There wasn’t anything in the documentation regarding this (I believe). I have added a method to the changed event of each replicator and can now see the exception and status message that it could not authorised.

            push.Changed += OnPushChanged;
            pull.Changed += OnPullChanged;

        private void OnPushChanged(object sender, ReplicationChangeEventArgs e)
            // e contains exceptions and statuses.

        private void OnPullChanged(object sender, ReplicationChangeEventArgs e)
           // e contains exceptions and statuses.


Here’s the code snippet to detect 401 errors

It’s not easy to find though. It might be better to have it in the Authentication guide


1 Like