{"action":"created","issue":{"url":"https://api.github.com/repos/pouchdb/pouchdb/issues/4364","labels_url":"https://api.github.com/repos/pouchdb/pouchdb/issues/4364/labels{/name}","comments_url":"https://api.github.com/repos/pouchdb/pouchdb/issues/4364/comments","events_url":"https://api.github.com/repos/pouchdb/pouchdb/issues/4364/events","html_url":"https://github.com/pouchdb/pouchdb/issues/4364","id":108431501,"number":4364,"title":"Authentication hook required inside live replication retry loop to support offline ->online -> offline -> online state changes","user":{"login":"tohagan","id":301525,"avatar_url":"https://avatars.githubusercontent.com/u/301525?v=3","gravatar_id":"","url":"https://api.github.com/users/tohagan","html_url":"https://github.com/tohagan","followers_url":"https://api.github.com/users/tohagan/followers","following_url":"https://api.github.com/users/tohagan/following{/other_user}","gists_url":"https://api.github.com/users/tohagan/gists{/gist_id}","starred_url":"https://api.github.com/users/tohagan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tohagan/subscriptions","organizations_url":"https://api.github.com/users/tohagan/orgs","repos_url":"https://api.github.com/users/tohagan/repos","events_url":"https://api.github.com/users/tohagan/events{/privacy}","received_events_url":"https://api.github.com/users/tohagan/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/pouchdb/pouchdb/labels/enhancement","name":"enhancement","color":"02d7e1"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":10,"created_at":"2015-09-26T01:15:51Z","updated_at":"2015-11-14T12:01:48Z","closed_at":null,"body":"Currently to implement cookie authentication (and potentially other token based authentications) we have a couple of implementation options:\r\n\r\n**Option 1**. login() then sync()\r\n\r\nI think this is what current [pouchdb-authentication](https://github.com/nolanlawson/pouchdb-authentication) db.login() example shows.\r\n\r\n- Perform cookie auth when **online**  (db.login())\r\n- Now start up live sync() that watches for when you go offline.\r\n\r\n- Assumes user is initially online when app starts up \r\n- Fails if they are initially offline (no hook to initiate db.login() in retry loop)\r\n\r\n**Option 2**.  Wait until the remote db is online then sync\r\n\r\n- Fails if the server or network subsequently goes offline.\r\n- sync() will retry and attempt to reconnect but cookie session may have timed out.\r\n\r\n**Option 3**. Perform login as a callback hook or authentication adapter \r\n\r\n- Register an authentication provider plugin (like an adapter) with remote db instance (note that different db instances may require different authentication so just registering it with PouchDb is not the best solution).\r\n- Start up live db.sync() or db.replicate() and supply authentication options as part of sync() or replication() options. \r\n- PouchDb replication retry loop now always initiates authentication (even the 1st time).\r\n\r\nNow works if app state changes from **offline** -> **online** -> **offline** -> **online** as the authentication is reapplied each time the replication goes online. We're no longer juggling multiple retry loops. \r\n\r\nIdea: If we're using [Cordova's Network Information plugin](https://github.com/apache/cordova-plugin-network-information), the replication retry loop might be able to leverage it's \"online\" event - or there might be a way for the app to inform the sync handle that this event has occurred. "},"comment":{"url":"https://api.github.com/repos/pouchdb/pouchdb/issues/comments/156690437","html_url":"https://github.com/pouchdb/pouchdb/issues/4364#issuecomment-156690437","issue_url":"https://api.github.com/repos/pouchdb/pouchdb/issues/4364","id":156690437,"user":{"login":"tohagan","id":301525,"avatar_url":"https://avatars.githubusercontent.com/u/301525?v=3","gravatar_id":"","url":"https://api.github.com/users/tohagan","html_url":"https://github.com/tohagan","followers_url":"https://api.github.com/users/tohagan/followers","following_url":"https://api.github.com/users/tohagan/following{/other_user}","gists_url":"https://api.github.com/users/tohagan/gists{/gist_id}","starred_url":"https://api.github.com/users/tohagan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tohagan/subscriptions","organizations_url":"https://api.github.com/users/tohagan/orgs","repos_url":"https://api.github.com/users/tohagan/repos","events_url":"https://api.github.com/users/tohagan/events{/privacy}","received_events_url":"https://api.github.com/users/tohagan/received_events","type":"User","site_admin":false},"created_at":"2015-11-14T12:01:48Z","updated_at":"2015-11-14T12:01:48Z","body":"I think I've answered Q1.   I've had a deeper read of the CouchDb replication and what I assume PouchDb does is emit a 'denied' event [replicate.js line 87](https://github.com/pouchdb/pouchdb/blob/master/lib/replicate/replicate.js#L87) for each bulk_docs write failure (e.g. validation docs exceptions). This only occurs once as the local seq number is then advanced so we don't retry replicating it - even if it failed with an error. \r\n\r\n"}}