401 Login Error on Sync Gateway Phonegap Plugin

122 views
Skip to first unread message

Vinay Bhinde

unread,
May 1, 2015, 7:14:43 AM5/1/15
to mobile-c...@googlegroups.com
Hello All,

I had a fully functional sync working with hybrid application on android and ios and cblite phonegap plugin. I use custom authentication for registering users with sync gateway .My app server returns me the cookie in setCookie header and then while performing replication i was manually setting the cookie header as follows :

var syncDefinitionPush = {
     source
: "budgetappdb",
     headers
: {
       
Cookie: "SyncGatewaySession=" + config.user.sessionId
     
},
     target
: {
       url
: REMOTE_SYNC_URL
     
},
     continuous
: true
   
};


var syncDefinitionPull = {
     target
: "budgetappdb",
     headers
: {
       
Cookie: "SyncGatewaySession=" + config.user.sessionId
     
},
     source
: {
       url
: REMOTE_SYNC_URL
     
},
     continuous
: true
   
};

While in normal scenarios when your sync gateway and your app server reside on the same domain then all the http requests should by default have a cookie header set once the app server has sent a setCookie header in response while performing custom authentication. But in case of cblite replication calls being made internally by cb lite to sync gateway, it didnt use the cookies and i was getting a 401 error while replicating on sync gateway. Hence i had to manually set the cookie header while  starting the replication process.

All above was specifically needed on android. cblite on iOS automatically uses the cookie to all subsequent calls made to sync_gateway after authenticating with my app server.

The main issue is that this stuff used to work on android with cblite plugin version 1.0.3. But recently  significant improvements were made in view indexing in android 1.0.4 and the build was given to us by one the team members. So we updated the plugin version to 1.0.4.Below is the thread where you can find the discussion and link to new plugin


The consequence was that now again cblite gets a 401 error on sync gateway while performing replication. Even though i have set custom cookie header while replicating still i guess somewhere in this new build the header is not being set and hence cookie is not being sent when cblite calls SG. Plus there is no way like in native API to setCookie so right now my sync does not work at all specially in android !

Any help how do i force cblite to use the SyncGatewaySession cookie when it makes http requests to SG for pull and push ?

Hideki Itakura

unread,
May 1, 2015, 4:44:13 PM5/1/15
to mobile-c...@googlegroups.com
Hi @Vinay,

CBL Android is not able to share cookie store, so an application needs to set cookie manually as you already done.

After 1.0.3/1.0.4 release, we reviewed how to set cookie from PhoneGap or REST API. We realized that headers item should be in the source or target item. See updated documentation for REST API _replicate (http://developer.couchbase.com/mobile/develop/references/couchbase-lite/rest-api/server/post-replicate/index.html#example)

So your JavaScript code should be like followings.
var syncDefinitionPush = {
     source
: "budgetappdb",

     target
: {
        headers: {
           
Cookie: "SyncGatewaySession=" + config.user.sessionId
       
},
        url: REMOTE_SYNC_URL
     
},
     continuous
: true
   
};

var syncDefinitionPull = {
     target
: "budgetappdb",
     

     source
: {
       headers: {
          
Cookie: "SyncGatewaySession=" + config.user.sessionId
       
},
       url: REMOTE_SYNC_URL
     
},
     continuous
: true
   
};

I hope this helps you.

Thanks,
Hideki

Vinay Bhinde

unread,
May 2, 2015, 12:58:05 AM5/2/15
to mobile-c...@googlegroups.com
Thank you @Hideki doing that change worked and the cookie is being sent successfully and sync works now.

There is one more issue that i guess is due to updating plugin to the build you gave. I use cblite's _changes feed with include_docs :true to update my application UI when doing push or pull. 

Below is the code for starting the _changes feed

config.db.changes({
     since
: config.info.update_seq,
     include_docs
: true
   
}, function(err, change) {
     console
.log(change);
   
var doc=change.doc;
})

Before updating the plugin i was able to get the entire document in that doc variable whenever a new doc was added or modified. But now after updating the plugin i get null always whenever any new is added or modified and because of it i am not able to update my UI whenever changes are made to cblite via replication.

Can u please check this ? Also i checked the docs to make sure i set the include_docs query param correctly while starting _changes feed and it seems correct. Any help will be appreciated.

Vinay Bhinde

unread,
May 2, 2015, 1:06:20 AM5/2/15
to mobile-c...@googlegroups.com
Also just an update which might help...

In case of documents added or modified locally i am able to get the entire document in _changes feed. The issue is when any doc is added or modified from remote (i.e Sync Gateway), in that case i do not get doc in _changes feed.

Hideki Itakura

unread,
May 2, 2015, 1:46:46 AM5/2/15
to mobile-c...@googlegroups.com
Hi Vinary,

I am glad to hear that my previous response helped you to address authentication issue.

Regarding ChangeEvent, in Native (Java) code, CBL return only documentID. You need to retrieve the document by using given documentID. I assume PhoneGap plugin behaves same.

DocumentChange Event (Navie/Java)

Can you post here output from `console.log(change);`?

If DocumentChange in Database.ChangeEvent does not contain documentID, and you are not able to get updated document, please file the ticket from https://github.com/couchbase/couchbase-lite-java-core/issues .

Hope this helps you.
Hideki

On Fri, May 1, 2015 at 10:06 PM, Vinay Bhinde <vin...@gmail.com> wrote:
Also just an update which might help...

In case of documents added or modified locally i am able to get the entire document in _changes feed. The issue is when any doc is added or modified from remote (i.e Sync Gateway), in that case i do not get doc in _changes feed.

--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/_faddosZ2as/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/e4f1c306-c8af-4ecd-963c-d911d2acecbf%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Thanks,
Hideki Itakura

Vinay Bhinde

unread,
May 2, 2015, 2:01:13 AM5/2/15
to mobile-c...@googlegroups.com
The inlcude_docs paramater's sole purpose is to bring along the entire document in the _changes feed so that we do not need to make an extra call to get the document from the doc_id. That is what i understand from the docs.


Below are the console.log(change) output in two different scenarios. 1st when the change made is local  and 2nd when change to cblite is from remote Sync Gateway

Console.log(change)  when document gets added to cblite from remote sync gateway (notice the doc attribute is null)
 
Object {docnullid"9f0f8c11-0c05-4905-a401-334c91695184"seq161changesArray[1]}
  1. changesArray[1]
    1. 0Object
      1. rev"1-09b5dff56c80ee1a8557f52022982da5"
      2. __proto__Object
    2. length1
    3. __proto__Array[0]
  2. docnull
  3. id"9f0f8c11-0c05-4905-a401-334c91695184"
  4. seq161
  5. __proto__Object
 

Console.log(change)  when document gets added to cblite locally from the mobile device itself

Object {docObjectid"ee89bc59-6a50-4ef3-b99d-faf0552323db"seq162changesArray[1]}
  1. changesArray[1]
    1. 0Object
    2. length1
    3. __proto__Array[0]
  2. docObject
    1. _id"ee89bc59-6a50-4ef3-b99d-faf0552323db"
    2. _rev"1-5eb7210d972afc0fd767659f430ce706"
    3. amt33
    4. channels"a...@gmail.com"
    5. cleartrue
    6. createdAt"2015-05-02T05:59:15.934Z"
    7. creditAccount"Checking"
    8. creditAccountKey"aa019602-da64-4a0f-8dec-5ab18b3f03ff"
    9. creditCat"Cash & Bank"
    10. creditSubCatnull
    11. date"Wed May 06 2015 00:00:00 GMT-0400 (EDT)"
    12. debitAccount"Bus"
    13. debitAccountKey"363fbea1-cd3f-42c4-b2d1-cf4b76dcc2ac"
    14. debitCat"Expense"
    15. debitSubCat"Auto"
    16. isBillfalse
    17. memo""
    18. notificationInfoObject
    19. parentCyclenull
    20. parentTransactionIdnull
    21. repeatnull
    22. splitnull
    23. totalAmtnull
    24. tranType"Expense"
    25. type"trans"
    26. unclearedAmtnull
    27. updatedAtnull
    28. __proto__Object
  3. id"ee89bc59-6a50-4ef3-b99d-faf0552323db"
  4. seq162
  5. __proto__Object


Hideki Itakura

unread,
May 2, 2015, 2:52:27 AM5/2/15
to mobile-c...@googlegroups.com
Hi Vinary,

Thank you for your log messages.

I read our code little deeper. With `include_docs` optional parameter with non-zero/false value, the response from GET /{db}/_changes must return document content. It seems current code works as expected. Currently I am not sure why the response does not include docs. Following links are code for GET /{db}/_changes. 

Event handler for GET /{db}/_changes

Code to generates response

Following could help us to identify the problem.
- Can you reproduce this issue with our ToDo Lite PhoneGap sample? https://github.com/couchbaselabs/TodoLite-PhoneGap
- Can you obtain SQLite (.cblite) file from device or emulator, and check revs table value?

Thanks,
Hideki



--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/_faddosZ2as/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchba...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages