Android: User Signup & Login

I am new to couchbase and I ahve been trying to find a tutorial about how to allow users to signup and login from my Android application. Any ideas, tutorials, examples?

There are different options for setting up authentication, see I suggest starting with basic auth and credentials hard coded in the config file :slight_smile:

thank you for your response.
I was hoping for something more complete, is there anything else?

Sorry, we don’t have any other materials that walk through the steps to setup sign up and login on Android from end to end. But happy to help with any questions you may have.

this might help you, FYI

1 Like

thank you very much!
there is an error, since the class NetworkHelper is missing from the project.
Have you tried to run it?

you can find the class detail in the README
Create a new java class called NetworkHelper with the following:

public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

Call post(String url, String json, Callback callback) {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder()
    Call call = client.newCall(request);
    return call;

1 Like

I ended up using this example as a guide but it is still not working. Below is the that I’m using:

public class SignupActivity extends AppCompatActivity implements View.OnTouchListener {

public static final String DB_NAME = "myapp";

private Database mDatabase = null;

@Bind( EditText _nameText;
@Bind( EditText _firstnameText;
@Bind( EditText _lastnameText;
@Bind( EditText _confirmpasswordText;
@Bind( EditText _emailText;
@Bind( EditText _passwordText;
@Bind( Button _signupButton;
@Bind( TextView _loginLink;
@Bind(    ScrollView _scrollLayout;

public void onCreate(Bundle savedInstanceState) {

    _signupButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

    _loginLink.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Finish the registration screen and return to the Login activity

    // Create a manager
    Manager manager = null;
    try {
        manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS);
    } catch (IOException e) {

    // Create or open the database named app
    Database database = null;
    try {
        database = manager.getDatabase(DB_NAME);
    } catch (CouchbaseLiteException e) {

    // The properties that will be saved on the document
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("name", _nameText);
    properties.put("password", _passwordText);
    properties.put("firstname", _firstnameText);
    properties.put("surname", _lastnameText);
    properties.put("email", _emailText);

    // Create a new document
    Document document = database.createDocument();

    // Save the document to the database
    try {
    } catch (CouchbaseLiteException e) {

    // Log the document ID (generated by the database)
    // and properties
    Log.d(DB_NAME, String.format("Document ID :: %s", document.getId()));
    Log.d(DB_NAME, String.format("Learning %s with %s %s %s %s", (String) document.getProperty("name"), (String) document.getProperty("password"),(String) document.getProperty("firstname"),
            (String) document.getProperty("surname"), (String) document.getProperty("email") ));

    // Create replicators to push & pull changes to & from Sync Gateway.
    URL url = null;
    try {
        url = new URL("http://serverurl:8080/REST-Model-context-root/resources/register");
    } catch (MalformedURLException e) {
    Replication push = database.createPushReplication(url);

    // Start replicators

public void signup() {

    if (!validate()) {


    final ProgressDialog progressDialog = new ProgressDialog(SignupActivity.this,
    progressDialog.setMessage("Creating Account...");;

    String name = _nameText.getText().toString();
    String email = _emailText.getText().toString();
    String password = _passwordText.getText().toString();

    new android.os.Handler().postDelayed(
            new Runnable() {
                public void run() {
                    // On complete call either onSignupSuccess or onSignupFailed
                    // depending on success
            }, 3000);

public void onSignupSuccess() {
    setResult(RESULT_OK, null);

public void onSignupFailed() {
    Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();


public boolean validate() {
    boolean valid = true;

    String name = _nameText.getText().toString();
    String email = _emailText.getText().toString();
    String password = _passwordText.getText().toString();

    if (name.isEmpty() || name.length() < 3) {
        _nameText.setError("at least 3 characters");
        valid = false;
    } else {

    if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
        _emailText.setError("enter a valid email address");
        valid = false;
    } else {

    if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
        _passwordText.setError("between 4 and 10 alphanumeric characters");
        valid = false;
    } else {

    return valid;

public boolean onTouch(View v, MotionEvent event) {

    return true;

Any ideas will be appreciated!!

Hi @stoumpos,
What does a part of your codes not work as you expect?

@hideki There are no errors but the users are not registered in the database

I believe registering user into the DB points to following codes.

I am not super familiar with ButterKnife SDK. In your codes variables start with _ are Android View. _nameText does not automatically returns String value in the TextEdit. I assume you should do EditText.getText().toString() for example _name.getText().toString()

I hope this helps.

I changed the code but unfortunately the result is the same…

I suggest starting with basic auth and credentials hard coded in the config file

I’m researching how to authenticate our users with Couchbase Mobile. Hard coding user and passwords in the config file doesn’t ring well in my ears; that’s potentially a huge security risk.

Is this the recommended way? I want to use a basic authentication with username and password.

True security on a mobile device is a myth (no matter how well you hide the keys to the door, if they are in the same place then someone will be able to find them eventually). That being said, there are things you can do to be reasonably secure.

For example, on Android you can make use of the built in encryption hardware, or at least the OS, to generate a per device encryption that you can use to store your password and username into the user preferences (assuming it is chosen at runtime).

If it is a static password, things are less easy because it will eventually have to either be stored somewhere in memory or on disk in unencrypted form in order to encrypt it for storage. I am not quite sure what to do in that situation.

I’m not familiar with Android, but it must have an API for securely storing passwords and other credentials. (The iOS equivalent is the Keychain.) Any Android tutorial/demo/sample app that shows how to authenticate with an online service will show how to use this, since the basic task of storing a username and password is the same.

The typical pattern on Android is to store the credentials in shared user preferences (as Jim suggested) and to encrypt it. The encryption key can be stored in the keystore. Before the keystore days, it was quite typical to use proguard obfuscation to hide the key.

Thank you, all, for your answers.

What about the sync gateway config? It’s all still a bit fuzzy.

I’ve looked through some the examples. My understanding is that you can users to the sync gateway config file to authenticate (and sync) a certain user and her data. I don’t really understand what users to add…

My application have hundreds of users, divided in to a few (say, 25) user groups. Can these groups be inserted as user entities in the sync gateway config, or is the general recommendation to treat each application user uniquely and add all to the sync gateway config file? Or should I create new replication users (with little or no connection to the application users at all)?

I wouldn’t add the users to the config file. Instead, use the admin REST API to add users.

And you probably want a separate user account for each application user.

Alright, I think I got most of it now. I have one follow-up question, though.

I’ve been looking into the SG REST API to authenticate my users. My understanding is that you can use OpenID to authenticate users with the /{db}/_oidc api. We don’t use any OpenID solution in our application at the moment. Is it possible to use a basic authentication with the /{db}/_user/ api instead? (We’ve fooled around with this API, and one issue we have faced is that the password-- hashed or not --is not returned).

Thank you very much.

OpenID isn’t required. You can specify a password when creating a user, and the client can log in via basic auth, or with cookies through the _session API.

one issue we have faced is that the password-- hashed or not --is not returned

Exposing the user’s password (even hashed) would be a security hole.