4
votes

Before I ask my question I would like to say thank you and also say sorry if this question is too simple or wrong for stack overflow but I really need some help.

Question:

I am using couchDB for the first time. I have created a registration form using HTML / AngularJS / Javascript now what I want to do is that a user can register and then login to view their personal information (i.e name, email, password) nobody can see that information expect the person to whom the account belongs to.

How do I create this in couchDB. How do I create/register users in couchDB and what is purpose of the already in-built "_users" database within couchDB, is the "_users" database where I create my users?

Again I apologise if I have given some wrong information or if this is not the right way of asking the question here but I kinda need some help. So please try to answer.

If you can provide with some basic sample code that would be helpful.

Thank you.

1

1 Answers

4
votes

what is purpose of the already in-built "_users" database within couchDB?

The _users database in couchdb is used to store the private information of users who will be managing different couchdb databases. Any user that you have in a _users database can be assigned as a member or an admin in the _security document of the databases.

For every user you can ask for a session from couchdb. couchdb will respond by sending you back a cookie. You can use that cookie in your own application.

For example this is the response that couchdb sends back when you authenticate:-

HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 43
Content-Type: application/json
Date: Mon, 03 Dec 2012 01:23:14 GMT
Server: CouchDB (Erlang/OTP)
Set-Cookie: AuthSession=cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw; Version=1; Path=/; HttpOnly

Note the path in the set cookie header. / means that the cookie is "operative" in the root of couchdb url (which by default is http://localhost:5984/). So the cookie will work in

http://localhost:5984/, http://localhost:5984/_something,http://localhost:5984/_something/_else etc

But if your application is hosted in http://localhost:3125/ then this cookie wont work. So you will need to use a middleware to extract this cookie and send it from your own application so that it is valid for your path or host your application within couchdb.

So to answer your question

  1. Store the user information in couchdb _users database.
  2. Query the session endpoint to generate a cookie.
  3. Reuse that cookie in your application by sending the set cookie header from your own application.