突然想到,他的basic auth,其實不是用來給系統用的,是給app用的
也就是app每一支都是用同樣的basicAuth id/pwd登入,區分 管理者和一般使用者

而一般使用者註冊後,帳密是另外開一個doucment,之後所有的文件管理是自己要控制的
跟mysql的一樣,不會給每個人有basic auth,權限管理,也是在sql下的時候,抓出資料做判斷

所以才會在 add user時,用restful curd,使用者代表不同的專案

==========
This is my think:

couchbase basic auth is not for every body login. It is app connect to sync_gateway for auth. Example is have normal user and Admin user. Maybe login sync_gateway, that get different document、channel、sync.

Normal user registered. The register data put in document, Maybe document name is “members” then system programmer need to control document show or not by permit. Like sql not everybody register, then get login mysql account. Only one account connect. After login, everythings is by program control.

So add user is use restful curd to sync_gateway. Like different project <= user.

 (basic auth : user must exist on Sync Gateway.)
https://forums.couchbase.com/t/couchbase-lite-and-couchbase-sync-gate-way-authentication-failed/3993
https://forums.couchbase.com/t/user-authentication-document-best-practices/3086

==========

curl -vX     POST

-H     ‘Content-Type: application/json’
-d     ‘{“name”: “guest”,   “password”: “xxxooo”}’
http://xxx.xxx.com:4985/db_name xxx ooo/_user/

use method POST

db_name xxx ooo is your db name

==========
when user login use basic auth website  (If sync_gateway guest disable)

http://sandk.mkinweb.com:4984/groupaccountingbooks/    This is login, you need to login by aa : aaa
http://sandk.mkinweb.com:4984/groupaccountingbooks/_session   If login ok, you can see this have

{"authentication\_handlers":\["default","cookie"\],"ok":true,"userCtx":{"channels":{"!":1},"name":"aa"}}

==========
See best new couchbase android todolite
ok~ see java    ui.login/LoginActivity

mBasicAuthButton = (Button)findViewById(R.id.basicAuthLoginButton);
then see layout : activity_login.xml   ok,find this:
android:id="@+id/basicAuthLoginButton"  

then you see 

android:visibility=“invisible” />  just remove  “invisible”.  When you login,  now you can see button.

But click no anythings.  Ok~ go back  ui.login/LoginActivity  find this:

private void promptUserForBasicAuthAndStartSync() {

see commit, now you can understand.  add user / pwd

curl -X POST http://localhost:4985/${db}/_user/ -d ‘{“name”:“foo”, “password”:“bar”}’

For Session

key point:  create one session

curl -X POST -H “Content-type: application/json” -d ‘{“name”: “your-user-id”}’ localhost:4985/todolite/_session

then sync_getway renturn session_id

{“session_id”:“03bdd7f1be83f035a7298924f9a28270feac7f4c”,“expires”:“2015-03-17T21:39:16.076186179Z”,“cookie_name”:“SyncGatewaySession”}

then chang session_id to 

private void loginWithCustomCookieAndStartSync() {

==========
sync_gate have username, password , this username, password for couchbae server, new bucket setting username, password

{
  “log”: [“CRUD”, “REST+”, “Access”],
  “interface”:":4984",
  “adminInterface”:“192.168.0.00:4985”,
  “databases”: {
     “groupaccountingbooks”: {
       “username”: “xxxxoooo”,
       “password”: “xxxxoooo”,
       “server”: “http://192.168.0.00:8091/”,
       “users”: {
          “GUEST”: {“disabled”: true, “admin_channels”: ["*"] }
        },
        “sync”: function(doc, oldDoc) { channel(“all_docs”); }
      }
    }
}