Search code examples
couchdbuser-management

What is the standard way to represent "business-logic users" in CouchDB?


I'm new to couchDB and still reading tutorials. My question is if it is the normal way to represent every user of my application as a new database user, as it seems to be explained that way everywhere I look?

Let's say I have an online game with many different players - would I create a new "database user" for every player who registers? Or would I make my own database "players" and create a sign-in logic in the app? Not being used to document-driven DB's it seems strange to me not to distinguish between db-users and users of my application...


Solution

  • You could do it either way. First about couchdb users

    • Users in couchdb are stored in a special _users database

    • Database permissions are handled by a special _security document. This is specific to every database.

    • In security documents you add users that you have already stored in the _users database previously.

    So you can certainly create a database per user. Before doing that ask yourself if the data that you store in each database is truly independent. Because you can't run map reduce queries across databases. So if you are planning to do aggregation across data for different users then this approach will not work.

    Couchdb can also help you with app level authentication. Since couchdb uses a cookie based authentication:

    1. Store your "business logic users" in the special _users database.
    2. Authenticate it with the _session endpoint.
    3. Extract the cookie header and sent it with your application headers.

    All the logic for authentication is implemented for you by couchdb. All you have got to do is manipulate headers. Send the cookie from your application and when authenticating with couchdb send it with couchdb's headers.

    If you prefer to write entire session management in your application that is fine too. In this case simply store the users in your database and verify that they exist before authenticating them. Like you would do with another database.

    The benefit of using couchdb is that it is secure by default --using pbkdf2 encryption scheme to encrypt passwords.