Mobile Authentication on UI

Hi guys, i have already create a sync gateway with 4 users and it works. Now, i am wondering how can i do authentication on the mobile side using those 4 users, this is not under replication method. The login is to access the application itself. I am using android by the way.

The login is to access the application itself

It sounds like you want the app to authenticate users before they can use it? You’d need to put up a modal username/password panel on launch. Then keep a separate CBL database per user, probably named after the username.

Hi jens, you are right. But i dont quite understand what is " separate CBL Database per user" What is CBL? Any links or example i could look at it?

Best Regards

CBL means “Couchbase Lite”

Righttttt, that was embarrassing. okay thanks:)

So are you saying i create one data bucket per user?

No, a database in Couchbase Lite. On the device (not on the server.)

Maybe you can describe in more detail what you’re trying to do?

I have 3 Users i have created in my config.json for my sync gateway for authentication. The screenshot shows my login in page. If i enter the wrong user name, my sync gateway will disallow the sync to happen as it is the wrong user name. However, my application will still continue to move on to the next page as there is no authentication at the client side.

SO i am wondering how can i use account i have created in my sync gateway to also authenticate my client side

Watch the state of the replication. If it stops with an error (with code 401), the login was incorrect.

what is the code to be able to read that?

Look at the API docs for the Replication class.

Hey Jens, thankf for the reply but i tried but it keep returning me the same value for both valid and invalid user name i not sure why. The sync gateway will reject it but my status will still be “true” for both valid and invalid user name

could you help me see if there is anything wrong with my code.

public boolean startReplications1(Database database,String Name) throws CouchbaseLiteException {
List channels = new ArrayList();
final Replication pull = database.createPullReplication(this.createSyncURL(false));
Authenticator authenticator = AuthenticatorFactory.createBasicAuthenticator(Name, “pass”);
boolean active = (pull.getStatus() == Replication.ReplicationStatus.REPLICATION_STOPPED);

return active;

The code looks alright except at the end — after you call start() the operation is asynchronous, so it’s going to be a while before status changes to stopped. You need to set an observer/listener for the replication status; I don’t know exactly what the Java API is for that, but it’s in the docs.

Also, why do you set active to true if the status is REPLICATION_STOPPED? Seems like it should be the other way around.

Hi Jens, thanks for you reply. ohhhh okay so i at the observer/listener before or after i call start() ?

I dont quite understand your statement here. I didnt not set anything to true. What i receive after the replication stopped in my above code is the boolean " True" . Could you clarify further if i am doing anything wrong?

My goal of this is to authenticate the user. If it is invalid , i get a boolean " False" and if its valid , i get a boolean " true". That is ultimately my main goal. Please advice:)

Create the observer before calling start. Then, every time it’s called you can check the status of the replication.

Could you modify my code above to help me out?

Sorry, I don’t have time to write code for people.

In general, you shouldn’t write a function that does what you’re trying to do above. The only way to have the function return a value indicating the final state of the replication is to make the function block until the replication completes. But you don’t want to block your thread for a long time because it’ll either stop the UI (if you’re doing this on the main thread) or other Couchbase Lite operations. Instead the function should only start the replication, and register an observer so it can later be notified about what happened and respond.

All our example apps show how to do this, although their observer methods mostly just log or display progress information.

Okay sure thanks for your help:) Just one more question, are you able to query the roles of a user and display in the UI? If so, what must i use?

No. Roles are part of Sync Gateway; clients don’t see them.

Code for detecting 401 unauthorized in replications is at