authentication problems accessing couchbase lite from jQuery

243 views
Skip to first unread message

Will Holcomb

unread,
May 16, 2014, 4:50:38 PM5/16/14
to mobile-c...@googlegroups.com
I am attempting to build a mobile app in Ember backed by CouchDB. I have things working with a regular server, but I get a 401 unauthorized error when attempting to connect to Couchbase Lite.

My code is pretty straightforward:

( function() {
    function setURL() {
        if( window.cblite ) {
            cblite.getURL( function( err, url ) {
                var adapter = App.__container__.lookup('store:main').adapterFor( 'application' )
                url = url.substring( 0, url.length - 1 ) // strip trailing /
                Ember.set( adapter, 'host', url )
            } )
        }
    }
    document.addEventListener( 'deviceready', setURL, false )
} )()

This gets a http://user:pass@localhost style url and sets it for the adapter, but subsequent requests to the database are rejected.

I have experienced HTTP Auth issues with xmlhttprequest before where the credentials seemed to be stripped when connecting to a port other than 80.

I was just wondering if anyone else was accessing a Couchbase Lite instance through jQuery, and if you had authentication issues.

-Will

J. Chris Anderson

unread,
May 16, 2014, 10:57:37 PM5/16/14
to mobile-c...@googlegroups.com
There is a similar discussion going on here: https://github.com/couchbaselabs/TodoLite-PhoneGap/issues/5

Are you able to get the TodoLite example app to work? It works for me, but I'm not a very large sample set...

Chris

Will Holcomb

unread,
May 18, 2014, 7:53:11 AM5/18/14
to mobile-c...@googlegroups.com
I was able to get the ToDo working. I tried using coax to access my db:

cblite.getURL( function( err, url ) {
  var coax = require( 'coax' )
  var db = coax( [url, 'wells'] )

  db.put( function( err, res ) {
    if( err ) {
      alert( "ip:" + err )
    }
    ⋮

This prints: "ip:Syntax error: Unexpected end of input". Creating a database like this is pretty much verbatim from the example; very frustrating…

-Will

Will Holcomb

unread,
May 18, 2014, 12:56:16 PM5/18/14
to mobile-c...@googlegroups.com
I've been doing some further testing. I wanted to try to see if I could get any connection to the server at all, so I added:

var server = coax( url )
server.get( function( err, res ) {
  if( err ) {
    alert( "bg:" + err )
  }
  alert( "gs:" + res )
} )

This returned: "bg:Syntax error: Unexpected end of input", the same as the put. To check if I could access it directly, I tried:

var xmlHttp = new XMLHttpRequest()
xmlHttp.open( 'GET', url, false )
xmlHttp.send( null )
alert( 'XMLHttpRequest get: ' +  xmlHttp.responseText )

This returns an ok status and welcome message. The coax methods which followed this now perform differently.

The get returns the same welcome message and the put to create the database now returns a HTTP 412: Precondition Failed meaning that one of the request headers couldn't be satisfied.

I'll keep digging…

-Will

J. Chris Anderson

unread,
May 19, 2014, 12:12:38 AM5/19/14
to mobile-c...@googlegroups.com
Thanks for digging. This is sort of new territory for us. We made it the default for the listener to require basic auth, so that other apps running on the same device can't access the http service.

There will likely be some gotcha's in opening the XHR connection, until we figure out the edge cases and document them.


On Sunday, May 18, 2014 9:56:16 AM UTC-7, Will Holcomb wrote:

The get returns the same welcome message and the put to create the database now returns a HTTP 412: Precondition Failed meaning that one of the request headers couldn't be satisfied.


This is expected -- the 412 means the database already exists, so it couldn't be created again. You can ignore 412 in this context.

Crhis
 

J. Chris Anderson

unread,
May 19, 2014, 7:38:50 PM5/19/14
to mobile-c...@googlegroups.com
It's strange to me that the Todo Lite example works for you but not other things. We have tracked it down to an issue with Android 4.x, which seems to be fixed in 4.4.2 (at least on my Nexus 7).

Can you tell us more about your Android version? 

Here is the bug report (there are a few others if you search the tracker for "basic auth") https://code.google.com/p/android/issues/detail?id=10307

I'm not sure what the workaround is yet -- maybe manually setting the Authorization header will do it. We are also trying some combinations of doing a GET before a PUT, etc, to see if we can box in the corner case.

Chris

Will Holcomb

unread,
May 20, 2014, 11:30:55 AM5/20/14
to mobile-c...@googlegroups.com
I'm testing on a Galaxy S3 running 4.3. I'm trying to get an emulator running 4.4.2 up, but it is not starting correctly.

The test code does a put for the database, which returns 412 meaning it already exists. Then a put for a design document at _design/reading which returns a 409, which I'm taking to mean a version already exists. Finally it does a get for _design/reading/_view/by_time. This returns an error: { error: 'not_found', reason: 'Router unable to route request to do_GET_DesignDocumentjava.lang.reflect.InvocationTargetException' }.

I have a link in the app that access that same url, /wells/_design/reading/_view/by_time, and it is returning a 404.

cblite.getURL( function( err, url ) {
  var adapter = App.__container__.lookup('store:main').adapterFor( 'application' )
  url = url.substring( 0, url.length - 1 )
  alert( url )
  Ember.set( adapter, 'host', url )

  // Without this I get: "ip: Syntax Error: Unexpected end of input"
  var xmlHttp = new XMLHttpRequest()
  xmlHttp.open( 'GET', url, false )
  xmlHttp.send( null )
  alert( 'XMLHttpRequest get: ' + xmlHttp.responseText )

  var coax = require( 'coax' )
  var db = coax( [url, 'wells'] )

  db.put( function( err, res ) {
    if( err && err.status != 412 ) {
      alert( "ip:" + err )
    } else {
      db.get( function( err, res ) {
        if( err ) {
          alert( "ig:" + err )
        } else {
          var design = "_design/reading"
          var views = {
            views: {
              by_time: {
                map: function( doc ) {
                  if( doc.type == 'reading' ) {
                    d = new Date( doc.time )
                    emit( [d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes()], doc )
                  }
                }.toString()
              }
            }
          }
          db.put( design, views, function( err, info ) {
            if( err && err.status != 409 ) {
              alert( "pv:" + err.status )
            } else {
              var view = db( [design, '_view'] )
              view.get( 'by_time', function( err, res ) {
if( err ) {
                  for( prop in err ) {
                    alert( "gv:" + prop + " : " + err[prop] )
                  }
                } else {
                  alert( res )
}
              } )
            }
          } )
        }
      } )
    }
  } )
} )

-Will



--
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/PCVFGaiTL5I/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/38adc762-2607-45b6-a356-2ad980f319a0%40googlegroups.com.

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

Will Holcomb

unread,
May 20, 2014, 11:56:34 AM5/20/14
to mobile-c...@googlegroups.com
I got the app running in a 4.4.2 emulator. I get the same error: Router unable to route request to do_GET_DesignDocumentjava.lang.reflect.InvocationTargetException.

-Will

J. Chris Anderson

unread,
May 20, 2014, 4:06:42 PM5/20/14
to mobile-c...@googlegroups.com


On Tuesday, May 20, 2014 8:56:34 AM UTC-7, Will Holcomb wrote:
I got the app running in a 4.4.2 emulator. I get the same error: Router unable to route request to do_GET_DesignDocumentjava.lang.reflect.InvocationTargetException.


The 409 conflict means the design doc isn't getting updated. So potentially it exists but doesn't have the views defined. What does the design document look like?

With your code above:

db.get(design,  function(err, ddoc){
  console.log(JSON.stringify(ddoc))
})

Will Holcomb

unread,
May 21, 2014, 1:17:32 PM5/21/14
to mobile-c...@googlegroups.com
Making sure the design document wasn't out of date was a good idea. Unfortunately, it seems correct:

{"views":{"by_time":{"map":"function ( doc ) { if( doc.type == 'reading' ) { d = new Date( doc.time ); emit( [d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes()], doc ) } }"}},"_rev":"1-f36be2417d00836fd5a375e7689fbda3","_id":"_design/reading"}

-Will


--
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/PCVFGaiTL5I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchba...@googlegroups.com.

J. Chris Anderson

unread,
May 21, 2014, 2:42:45 PM5/21/14
to mobile-c...@googlegroups.com


On Wednesday, May 21, 2014 10:17:32 AM UTC-7, Will Holcomb wrote:
Making sure the design document wasn't out of date was a good idea. Unfortunately, it seems correct:

{"views":{"by_time":{"map":"function ( doc ) { if( doc.type == 'reading' ) { d = new Date( doc.time ); emit( [d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes()], doc ) } }"}},"_rev":"1-f36be2417d00836fd5a375e7689fbda3","_id":"_design/reading"}

-Will

Hmm, I'm running out of ideas. What is the value of:

var view = db( [design, '_view'] )
console.log(view.url)

If that doesn't show something unexpected, my next step will be to ask if I can try to run your code on my workstation...

Cheers,
Chris

 
 

Will Holcomb

unread,
May 23, 2014, 11:56:48 AM5/23/14
to mobile-c...@googlegroups.com
The program that I was originally working on has to be reviewed before it's source is opened. I created a new cordova project, imported the couchbase lite plugin, copied over my code, and it runs correctly.

I at a loss as to why, but if I figure out what changed, I'll let you know.

In any case I now have a working Ember app running on a mobile device backed by couchbase lite. If anyone would like to try it out, the source is at: https://github.com/wholcomb/habit_tracker A version backed by cloudant is running at: http://hbit.herokuapp.com

-Will


--
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/PCVFGaiTL5I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchba...@googlegroups.com.

Traun Leyden

unread,
May 27, 2014, 12:00:52 PM5/27/14
to mobile-c...@googlegroups.com
Have you tried getting TodoLitePhonegap running?

If you can get that working, you can then compare things against your app to isolate the issue.

If you can't, then it will be easier for Chris and I to debug the issue.


--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/CAB8ffCA0-SU0zo6%3D3tYpCSX9ficci7bnGOzw7jV%2BZWeVVPHAuw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages