From 79a9e12d60e66df7fc903bc7f65d6ef15af7c281 Mon Sep 17 00:00:00 2001 From: Joren l'Ami Date: Fri, 19 Aug 2016 15:33:59 +0200 Subject: [PATCH 1/8] emit loggedin on pouch db after restore and authenticate success --- addon/authenticators/pouch.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/addon/authenticators/pouch.js b/addon/authenticators/pouch.js index d2b977a..4fec96a 100644 --- a/addon/authenticators/pouch.js +++ b/addon/authenticators/pouch.js @@ -3,6 +3,8 @@ import Ember from 'ember'; const { getOwner } = Ember; export default Base.extend({ + store: Ember.inject.service(), + init() { this._super(...arguments); @@ -11,7 +13,7 @@ export default Base.extend({ //let the user override the default adapter let pouchAdapterName = config.emberPouch.authAdapter || 'user'; - let pouchAdapter = getOwner(this).lookup(`adapter:${pouchAdapterName}`); + let pouchAdapter = this.get('store').adapterFor(pouchAdapterName); Ember.assert('You must have an ember-pouch adapter setup for authentication', pouchAdapter); @@ -19,16 +21,24 @@ export default Base.extend({ }, restore(data) { - return this.db.getUser(data.name).then(function() { return data; }); + let self = this; + return this.db.getUser(data.name).then(function() { + self.db.emit('loggedin'); + return data; + }); }, authenticate(username, password) { + let self = this; return this.db.login(username, password, { ajax: { headers: { 'X-Hello': 'World' } } + }).then(function(data) { + self.db.emit('loggedin'); + return data; }); }, From f98c0f9543c7c8051296295b906a1fef13cbd4ec Mon Sep 17 00:00:00 2001 From: Joren l'Ami Date: Fri, 26 Aug 2016 13:48:17 +0200 Subject: [PATCH 2/8] loginOpts for chrome fix logout emit getSession instead of getUser for restore --- addon/authenticators/pouch.js | 36 ++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/addon/authenticators/pouch.js b/addon/authenticators/pouch.js index 4fec96a..93e2bc1 100644 --- a/addon/authenticators/pouch.js +++ b/addon/authenticators/pouch.js @@ -5,13 +5,24 @@ const { getOwner } = Ember; export default Base.extend({ store: Ember.inject.service(), + loginOpts: {}, + init() { this._super(...arguments); let config = getOwner(this).resolveRegistration('config:environment'); //let the user override the default adapter - let pouchAdapterName = config.emberPouch.authAdapter || 'user'; + let pouchAdapterName = config.authAdapter || 'user'; + if (config.pouchDb && config.pouchDb._fixChromeLoginBug) {//not needed for cloudant + this.loginOpts = { + ajax: { + headers: { + 'X-Hello': 'World' + } + } + }; + } let pouchAdapter = this.get('store').adapterFor(pouchAdapterName); @@ -22,27 +33,30 @@ export default Base.extend({ restore(data) { let self = this; - return this.db.getUser(data.name).then(function() { - self.db.emit('loggedin'); - return data; + return this.db.getSession().then(function(resp) { + let result = null; + if (!Ember.isEmpty(data.name) && resp.userCtx.name === data.name) { + result = data; + self.db.emit('loggedin'); + } + else { + result = Ember.RSVP.reject("Not logged in or incorrect user in cookie"); + } + + return result; }); }, authenticate(username, password) { let self = this; - return this.db.login(username, password, { - ajax: { - headers: { - 'X-Hello': 'World' - } - } - }).then(function(data) { + return this.db.login(username, password, this.loginOpts).then(function(data) { self.db.emit('loggedin'); return data; }); }, invalidate() { + this.db.emit('loggedout'); return this.db.logout(); } }); From cea648672e579d68ebf16218ddbfc60e4c9d253f Mon Sep 17 00:00:00 2001 From: Joren l'Ami Date: Mon, 29 Aug 2016 11:15:56 +0200 Subject: [PATCH 3/8] beta 2 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 62fb60a..ec1fe4a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ember-simple-auth-pouch", - "version": "0.1.0-beta.1", + "version": "0.1.0-beta.2", "description": "Ember Simple Auth Authenticator for Pouch.", "directories": { "doc": "doc", @@ -36,7 +36,7 @@ "ember-export-application-global": "^1.0.4", "ember-pouch": "3.0.0", "ember-resolver": "^2.0.3", - "ember-simple-auth": "1.0.1", + "ember-simple-auth": "1.1.0", "ember-try": "~0.0.8" }, "keywords": [ From 94e428b61baf83c6290fb996fa3eb527fc9978aa Mon Sep 17 00:00:00 2001 From: Joren l'Ami Date: Thu, 8 Sep 2016 16:00:51 +0200 Subject: [PATCH 4/8] _session GET after POST, to circumvent bug https://github.com/apache/couchdb-couch/pull/16 --- addon/authenticators/pouch.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/addon/authenticators/pouch.js b/addon/authenticators/pouch.js index 93e2bc1..1c3a455 100644 --- a/addon/authenticators/pouch.js +++ b/addon/authenticators/pouch.js @@ -50,8 +50,10 @@ export default Base.extend({ authenticate(username, password) { let self = this; return this.db.login(username, password, this.loginOpts).then(function(data) { - self.db.emit('loggedin'); - return data; + return self.db.getSession().then(function(resp) { + self.db.emit('loggedin'); + return resp.userCtx; + }); }); }, From e1e1089ee9050192866e545de421c54f5cd46078 Mon Sep 17 00:00:00 2001 From: Joren l'Ami Date: Thu, 8 Sep 2016 16:12:22 +0200 Subject: [PATCH 5/8] no user model and adapter anymore, use config.authAdapter, with 'application' as default --- addon/adapters/user.js | 59 ----------------------------------- addon/authenticators/pouch.js | 2 +- addon/models/user.js | 6 ---- app/adapters/user.js | 1 - app/models/user.js | 1 - 5 files changed, 1 insertion(+), 68 deletions(-) delete mode 100644 addon/adapters/user.js delete mode 100644 addon/models/user.js delete mode 100644 app/adapters/user.js delete mode 100644 app/models/user.js diff --git a/addon/adapters/user.js b/addon/adapters/user.js deleted file mode 100644 index 5c54a08..0000000 --- a/addon/adapters/user.js +++ /dev/null @@ -1,59 +0,0 @@ -import { Adapter } from 'ember-pouch'; -import PouchDB from 'pouchdb'; -import Ember from 'ember'; -const { getOwner } = Ember; -//import DS from 'ember-data'; - -const { assert, isEmpty } = Ember; - -export default Adapter.extend({ - init() { - this._super(...arguments); - - let config = getOwner(this).resolveRegistration('config:environment'); - let authHost = config.emberPouch.authHost; - let userDoc = '_users'; - - assert('config.emberPouch.authHost must be set in your config/environment.js file', !isEmpty(authHost)); - //let secureRegex = /https/; - //assert('Your remote db should be secure!', secureRegex.test(authHost)); - - let authDb = authHost;//+'/'+userDoc; - - let db = new PouchDB(authDb, {skipSetup: true}); - if(config.emberPouch.syncUsers) { - let local = new PouchDB(userDoc); - local.sync(db, {live: true, retry: true}) - .on('error', console.log.bind(console)); - } - - this.set('db', db); - }, - - findRecord(store, type, id){ - return this.get('db').getUser(id).then(function(data) { - data.id = data._id; - data.rev = data._rev; - delete data._id; - delete data._rev; - return {user: data}; - }); - }, - createRecord(){}, - updateRecord(store, type, snapshot){ - let id = snapshot.id.substr("org.couchdb.user:".length); - let meta = snapshot.record.toJSON(); - delete meta.rev; - delete meta.name; - return this.get('db').putUser(id, {metadata: meta}); - }, - deleteRecord(){}, - findAll(){ - - }, - findQuery(){ - throw new Error( - "findQuery not yet supported by ember-simple-auth-pouch" - ); - } -}); diff --git a/addon/authenticators/pouch.js b/addon/authenticators/pouch.js index 1c3a455..b2c0686 100644 --- a/addon/authenticators/pouch.js +++ b/addon/authenticators/pouch.js @@ -13,7 +13,7 @@ export default Base.extend({ let config = getOwner(this).resolveRegistration('config:environment'); //let the user override the default adapter - let pouchAdapterName = config.authAdapter || 'user'; + let pouchAdapterName = config.authAdapter || 'application'; if (config.pouchDb && config.pouchDb._fixChromeLoginBug) {//not needed for cloudant this.loginOpts = { ajax: { diff --git a/addon/models/user.js b/addon/models/user.js deleted file mode 100644 index c13eaba..0000000 --- a/addon/models/user.js +++ /dev/null @@ -1,6 +0,0 @@ -import DS from 'ember-data'; -import { Model } from 'ember-pouch'; - -export default Model.extend({ - -}); diff --git a/app/adapters/user.js b/app/adapters/user.js deleted file mode 100644 index 80c7601..0000000 --- a/app/adapters/user.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from 'ember-simple-auth-pouch/adapters/user'; diff --git a/app/models/user.js b/app/models/user.js deleted file mode 100644 index 1d85c52..0000000 --- a/app/models/user.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from 'ember-simple-auth-pouch/models/user'; From c7c804956fe5e0704f053939c7a24669fc041023 Mon Sep 17 00:00:00 2001 From: Joren l'Ami Date: Fri, 9 Sep 2016 09:53:03 +0200 Subject: [PATCH 6/8] removed _fixChromeLoginBug and separated getDb() call to be overridden in app --- addon/authenticators/pouch.js | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/addon/authenticators/pouch.js b/addon/authenticators/pouch.js index b2c0686..047e07f 100644 --- a/addon/authenticators/pouch.js +++ b/addon/authenticators/pouch.js @@ -4,31 +4,24 @@ const { getOwner } = Ember; export default Base.extend({ store: Ember.inject.service(), - - loginOpts: {}, - + init() { this._super(...arguments); - let config = getOwner(this).resolveRegistration('config:environment'); + this.db = this.getDb(); + }, + + getDb() { + let config = getOwner(this).resolveRegistration('config:environment'); //let the user override the default adapter let pouchAdapterName = config.authAdapter || 'application'; - if (config.pouchDb && config.pouchDb._fixChromeLoginBug) {//not needed for cloudant - this.loginOpts = { - ajax: { - headers: { - 'X-Hello': 'World' - } - } - }; - } let pouchAdapter = this.get('store').adapterFor(pouchAdapterName); Ember.assert('You must have an ember-pouch adapter setup for authentication', pouchAdapter); - - this.db = pouchAdapter.db; + + return pouchAdapter.db; }, restore(data) { @@ -49,7 +42,7 @@ export default Base.extend({ authenticate(username, password) { let self = this; - return this.db.login(username, password, this.loginOpts).then(function(data) { + return this.db.login(username, password).then(function() { return self.db.getSession().then(function(resp) { self.db.emit('loggedin'); return resp.userCtx; From 2b2ef0ec2717199f6e85b3234da8754f324111eb Mon Sep 17 00:00:00 2001 From: Joren l'Ami Date: Wed, 14 Sep 2016 10:38:12 +0200 Subject: [PATCH 7/8] pouchdb-authentication after-install ^0.5.3 --- blueprints/ember-cli-simple-auth-pouch/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blueprints/ember-cli-simple-auth-pouch/index.js b/blueprints/ember-cli-simple-auth-pouch/index.js index 7a0d158..a763978 100644 --- a/blueprints/ember-cli-simple-auth-pouch/index.js +++ b/blueprints/ember-cli-simple-auth-pouch/index.js @@ -13,7 +13,7 @@ module.exports = { afterInstall: function(options) { return this.addBowerPackagesToProject([ - { name: "pouchdb-authentication", target: "^0.4.1" } + { name: "pouchdb-authentication", target: "^0.5.3" } ]); } }; From 767525bfdfa38117b48c0b96f247001a269a0136 Mon Sep 17 00:00:00 2001 From: Joren l'Ami Date: Wed, 14 Sep 2016 10:38:29 +0200 Subject: [PATCH 8/8] repository info --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ec1fe4a..972250a 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "start": "ember server", "test": "ember try:testall" }, - "repository": "", + "repository": "https://github.com/martinic/ember-simple-auth-pouch", "engines": { "node": ">= 0.10.0" },