Nevermind, I finally managed to get the pouchdb replication to sync gateway in cordova / xwalk working after days of trial and error. Basically return the cookie details as part of the json login response, so in my case that’s in the session property in the code snippet above… then add that cookie info to your pouchdb opts in a custom header that won’t complain about being set:
var opts = {
since: 0,
include_docs: false,
attachments: false,
crossDomain: true,
withCredentials: true,
skipSetup: true,
ajax: {
headers: {
'Authorization': SessionService.getAuthorizationHeader(),
'ProxyCookie': SessionService.getCookieHeader()
}
};
Note you can also add the ProxyCookie to all outgoing $http requests via a custom interceptor if you want consistency, but it’s not needed.
You need to replicate to a custom REST endpoint that will proxy sync gateway. I’m using node and express, and I setup my cors on this to accept the ProxyCookie header among the other ones like Authorization etc.
My api/sync route looks something like this:
'use strict';
var express = require('express'),
router = express.Router(),
config = require('module/config'),
httpProxy = require('http-proxy'),
syncProxy = httpProxy.createProxyServer({
target: config.SG_CONFIG.PUBLIC_ENDPOINT,
xfwd: true
});
syncProxy.on('proxyReq', function (proxyReq, req, res, options) {
// Cookie workaround for cordova / xwalk
var proxycookie = req.headers.proxycookie;
if (proxycookie) {
proxyReq.setHeader('Cookie', proxycookie);
}
});
syncProxy.on('error', function (e) {
console.log('\n\nsyncProxy error', e);
});
router.all('*', function (req, res) {
syncProxy.web(req, res);
});
module.exports.router = router;
It’s really important to define this root BEFORE the body parser if you are using it, this tripped me up for awhile.
// Add cookie parser middleware
router.use(cookieParser());
// Add sync / proxy route before body parser middleware
router.use('/sync', require('./sync').router);
// Add json body parser middleware
router.use(bodyParser.urlencoded({'extended': 'true'}));
router.use(bodyParser.json());
router.use(bodyParser.json({type: 'application/vnd.api+json'}));
Finally in my sync gateway config I setup my cors headers like so:
"headers": ["Accept", "Authorization", "Content-Type", "Origin", "Referer", "X-CSRF-Token"],
I think that was it. I hope this saves someone else some time, as I’ve lost a few days trying to get cookies working in cordova / xwalk for authenticating sync gateway replication. I really wish it supported JWT.