Ember Simple Auth Pouch
This is Ember addon is an extension to the Ember Simple Auth library that provides a way to authenticate to a CouchDB server using pouchdb-authentication.
Because user's credentials and tokens are exchanged between the Ember.js app and the server, you must use HTTPS for this connection!
Installation
Ember Simple Auth Token can be installed with Ember CLI by running:
ember install ember-simple-auth-pouch
Setup
Authenticator
In order to use the pouch authenticator, the application should have a route for login. In most cases, the login route will display a form with a username
and password
field. On form submit, the authenticate
action will be called on the session
:
// app/router.jsRouter;
{{! app/templates/login.hbs }} Login {{input id='username' placeholder='Enter Login' value=username}} Password {{input id='password' placeholder='Enter Password' type='password' value=password}} Login {{#if errorMessage}} {{errorMessage}} {{/if}}
// app/controllers/login.js;;; @service session; @action { const target = event; let identification = targetvalue; let password = targetvalue; event; thissession; }
Pouch Authenticator
Include
// config/environment.jsENVauthAdapter = 'application';
Setup the pouch authenticator
// app/authenticators/pouchjs; { let pouchAdapter = thisstore;//getOwner(this).lookup(`adapter:${pouchAdapterName}`); return pouchAdapterremoteDb; }
Authenticated routes
// app/routes/secret.js;; // do your secret model setup here
Using Session service
Invalidate session when logged out remote:
// app/adapters/application.js;;;;;;; PouchDB; @service session; @service cloudState; @service refreshIndicator; { super...arguments; const localDb = configlocal_couch || 'blogger'; ; const db = localDb; thisdb = db; // If we have specified a remote CouchDB instance, then replicate our local database to it if configremote_couch const remoteDb = configremote_couch { optscredentials = 'include'; return PouchDB; } ; const replicationOptions = live: true retry: true ; dbreplicate; dbreplicate; thisremoteDb = remoteDb; return this; } { thisrefreshIndicator; let store = thisstore; let recordTypeName = this; thisdbrel; });
Sample app
Tom Dale's blog example using Ember CLI and ember-simple-auth-pouch: broerse/ember-cli-blog
Credits
And of course thanks to all our wonderful contributors, here! and especially @mattmarcum for creating this addon.
Changelog
- 0.2.0 - Switch to import 'pouchdb-authentication' in App
- 0.1.0 - Release v0.1.0
- 0.1.0-beta.7 - no .db, but use getDb() everywhere
- 0.1.0-beta.6 - use getDb()
- 0.1.0-beta.5 - use ember-pouch 5.0.0-beta.2
- 0.1.0-beta.4 - use response when restoring session
- 0.1.0-beta.3 - Add peerDependencies
- 0.1.0-beta.2 - Switch to ember-cli 2.7.0
- 0.1.0-beta.1 - First Beta release