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 d2b977a..047e07f 100644 --- a/addon/authenticators/pouch.js +++ b/addon/authenticators/pouch.js @@ -3,36 +3,55 @@ import Ember from 'ember'; const { getOwner } = Ember; export default Base.extend({ + store: Ember.inject.service(), + 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.emberPouch.authAdapter || 'user'; + let pouchAdapterName = config.authAdapter || 'application'; - 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); - - this.db = pouchAdapter.db; + + return pouchAdapter.db; }, restore(data) { - return this.db.getUser(data.name).then(function() { return data; }); + let self = this; + 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) { - return this.db.login(username, password, { - ajax: { - headers: { - 'X-Hello': 'World' - } - } + let self = this; + return this.db.login(username, password).then(function() { + return self.db.getSession().then(function(resp) { + self.db.emit('loggedin'); + return resp.userCtx; + }); }); }, invalidate() { + this.db.emit('loggedout'); return this.db.logout(); } }); 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'; 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" } ]); } }; diff --git a/package.json b/package.json index 62fb60a..972250a 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", @@ -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" }, @@ -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": [