Has anyone got pouchdb replicating with the latest 1.1 sync gateway? CB Lite JS?

Has anyone got pouchdb replicating with the latest 1.1 sync gateway?

Using this version: https://registry.hub.docker.com/u/couchbase/sync-gateway/

The browser keeps prompting me for a username and password when I try to replicate. Is the endpoint, sync.domain.com/bucket/, or should it be sync.domain.com/bucket/(_sync, _replicate, or something else). I can see that the browser and the pouchdb ajax request have the Sync Gateway Session cookie set that was returned from the successful registration / login. The pouchdb docs are a bit unclear if it works or not, as one place says it’s supported, and the other we’re working on it. I heard noise somewhere that the CB team might be working on a CB Lite solution for JS, is that true?

Yes PouchDB compat was released in 1.1.
Do you see any error message in the browser console or SG logs to help troubleshoot the issue?

This tutorial might help.

James

I “think” the root of the problem might be that sync gateway is ignoring my SyncGatewaySession cookie as it thinks hasn’t originated from the right location, due to my dev / testing setup. From the docs: “For the cookie to be recognized, your site must be configured so that your app’s API and the gateway appear on the same public host name and port.”

See this line: https://github.com/couchbase/sync_gateway/blob/d0d514107b60cf56b19aa5089306de06459cbf3b/src/github.com/couchbase/sync_gateway/rest/routing.go#L229

My sync gateway runs on sync.mydomain.com, port 80 and is obviously also available on localhost from that box. The current sg config is this gist: https://gist.github.com/derekdon/349ff3d95197e2d66578. I’ve just added some CORS entries in an effort to make this work.

"CORS": {
   "Origin": ["http://localhost:8080", "http://localhost:9000", "http://localhost:3000"],
   "LoginOrigin": ["http://localhost:8080", "http://localhost:9000", "http://localhost:3000"],
   "Headers": ["Content-Type"],
   "MaxAge": 17280000
}

Current dev / testing setup and flow:

I ssh into the box with port forwarding to the sg ports.

ssh d2 -L localhost:4985:localhost:4985 -L localhost:4984:localhost:4984

1: Ionic / Angular running on localhost:8080 on my laptop.
2: App makes a request to a local cors enabled node server running on localhost:3000 to register a user.
3: Node app does some custom auth stuff, sets a jwt, and posts to sg http://localhost:4985/mybucket/_user which creates the user just fine. It then posts to sg http://localhost:4985/mybucket/_session to create a session just fine, and sets the resulting cookie on the response header.
4: Now my app can use privileged apis due to the Bearer token, and I can see the Cookie in the header… So now I can create some local pouchdb docs, and try to replicate to sg, and on doing so always get prompted for a login, the same way I would if I visited sync.mydomain.com/mybucket directly via the browser.

I tied replicating the app local pouchdb to http://localhost:4984/mybucket and to sync.mydomain.com/mybucket. (didn’t really expect the latter to work anyway.)

What I really want and what I’ve started to do now is create a proxy in the node app to sg. So the ionic app hits http://localhost:3000/api/v0/auth to login / register, followed by http://localhost:3000/api/v0/sync to replicate. To proxy the requests I’ve installed node-http-proxy, and I’m hoping to be able to route all the syncing via this and update the headers if needed.

'use strict';

var express = require('express'),
    router = express.Router(),
    config = require('module/config'),
    httpProxy = require('http-proxy'),
    syncProxy = httpProxy.createProxyServer();

// To modify the proxy connection before data is sent
syncProxy.on('proxyReq', function(proxyReq, req, res, options) {
    // Try anything...
    proxyReq.setHeader('Host', 'http://localhost:8080');
    proxyReq.setHeader('Origin', 'http://localhost:8080');
    proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});

router.all('/*', function(req, res){
    syncProxy.web(req, res, { target: config.SG_CONFIG.PUBLIC_ENDPOINT});
});

module.exports = router;

So if I just use the REST client in intellij with the Cookie set manually (just grabbing them from the browser), and do a GET on http://localhost:3000/api/v0/sync, I get:

HTTP/1.1 401 Unauthorized
X-Powered-By: Express
Vary: Origin
access-control-allow-credentials: true
access-control-allow-headers: Content-Type
access-control-allow-origin: http://localhost:3000 (<< This can be 8080 either depending on what I try...)
content-type: application/json
server: Couchbase Sync Gateway/unofficial
www-authenticate: Basic realm="Couchbase Sync Gateway"
date: Thu, 23 Jul 2015 21:40:34 GMT
content-length: 50
connection: close

I know this is a bit funky re port forwarding etc… but I don’t want to have to deploy it to see it working if I can help it. Any ideas?

When trying to replicate directly to SG (not via my node proxy) after a successful login… this is the response I get (copied from Chrome Dev Tools):

General
Remote Address:[::1]:4984
Request URL:http://localhost:4984/mm/?_nonce=1437745677400
Request Method:GET
Status Code:401 Unauthorized
Response Headers
view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Origin:http://localhost:8080
Content-Length:50
Content-Type:application/json
Date:Fri, 24 Jul 2015 13:47:57 GMT
Server:Couchbase Sync Gateway/unofficial
Www-Authenticate:Basic realm="Couchbase Sync Gateway"
Request Headers
view source
Accept:application/json
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Cookie:SyncGatewaySession=j%3A%7B%22session_id%22%3A%22fef866 (removed for post) 7a6e139%22%2C%22expires%22%3A%222015-07-25T13%3A42%3A11.647262867Z%22%2C%22cookie_name%22%3A%22SyncGatewaySession%22%7D
Host:localhost:4984
Origin:http://localhost:8080
Referer:http://localhost:8080/index.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36
Query String Parameters
view source
view URL encoded
_nonce:1437745677400

Server log snippet:

Jul 24 13:47:57 mm-database-2 bash[10066]: 2015-07-24T13:47:57.495Z HTTP: #226: GET /mm/?_nonce=1437745677400
Jul 24 13:47:57 mm-database-2 bash[10066]: 2015-07-24T13:47:57.495Z HTTP: #226: --> 401 Login required (1.7 ms)

When just doing a http get to SG via my node proxy after a successful login… this is the response I get (copied from Chrome Dev Tools):

OPTIONS

General
Remote Address:[::1]:3000
Request URL:http://localhost:3000/api/v0/sync
Request Method:OPTIONS
Status Code:204 No Content
Response Headers
view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Content-Type,Authorization,Accept,X-Requested-With
Access-Control-Allow-Methods:GET,HEAD,PUT,PATCH,POST,DELETE
Access-Control-Allow-Origin:http://localhost:8080
Connection:keep-alive
Date:Fri, 24 Jul 2015 14:02:58 GMT
Vary:Origin
X-Powered-By:Express
Request Headers
view source
Accept:/
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:3000
Origin:http://localhost:8080
Referer:http://localhost:8080/index.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36

GET

General
Remote Address:[::1]:3000
Request URL:http://localhost:3000/api/v0/sync
Request Method:GET
Status Code:401 Unauthorized
Response Headers
view source
access-control-allow-credentials:true
access-control-allow-headers:Content-Type
access-control-allow-origin:http://localhost:8080
connection:close
content-length:50
content-type:application/json
date:Fri, 24 Jul 2015 14:02:59 GMT
server:Couchbase Sync Gateway/unofficial
Vary:Origin
www-authenticate:Basic realm="Couchbase Sync Gateway"
X-Powered-By:Express
Request Headers
view source
Accept:application/json, text/plain, /
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhb (removed for post) Uj9wgnkzIPe5lsiX7CxnbE
Connection:keep-alive
Cookie:SyncGatewaySession=j%3A%7B%22session_id%22%3A%22 (removed for post) %22expires%22%3A%222015-07-25T13%3A53%3A57.231224771Z%22%2C%22cookie_name%22%3A%22SyncGatewaySession%22%7D
Host:localhost:3000
Origin:http://localhost:8080
Referer:http://localhost:8080/index.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36

Server logs:

Jul 24 14:02:59 mm-database-2 bash[10066]: 2015-07-24T14:02:59.123Z HTTP: #229: GET /mm/
Jul 24 14:02:59 mm-database-2 bash[10066]: 2015-07-24T14:02:59.124Z HTTP: #229: --> 401 Login required (5.1 ms)

Actually never mind! I realised that the session cookie was in the wrong format. Doh!

Link is not available.

what is the correct format… am also facing same issue…