Slide 1

Slide 1 text

Integrate User Management and Cloudant Query into a Node.js & Cloudant Application IBM Cloudant Webinar Thursday, June 18, 2015 Bradley Holt, Developer Advocate @BradleyHolt

Slide 2

Slide 2 text

2

Slide 3

Slide 3 text

Location Tracker •  Stores data locally in PouchDB •  Front end built with AngularJS •  Authentication logic built with Node.js •  User interface built with Leaflet •  Replicates location data to Cloudant •  More info: https://cloudant.com/location-tracker/ 3

Slide 4

Slide 4 text

PouchDB •  A database in your web browser •  Also works in Node.js •  Can synchronize with any database that implements the CouchDB Replication Protocol 4

Slide 5

Slide 5 text

Location Tracker: Parts 1 & 2 •  Part 1 •  Uses HTML5 geolocation API •  Uses PouchDB and Cloudant •  Part 2 •  Polished single-page app •  Introduces AngularJS •  Both implemented as CouchApps •  Self-contained, hosted within Cloudant •  No middle tier 5

Slide 6

Slide 6 text

Adding a Middle Tier •  CouchApps only have a data and a presentation tier •  Practical limitations to application logic within Cloudant/CouchDB •  For example, authentication requires a logic/application tier 6 Image Credit: Overview of a three-tier application, by Bartledan, based on a file by User:Foofy.Bartledan at en.wikipedia [Public domain], from Wikimedia Commons

Slide 7

Slide 7 text

Location Tracker: Part 3 •  Authentication and authorization •  User registration •  User login •  User logout •  Map of individual user locations and movement •  Static web server and reverse proxy to Cloudant •  Deployable to IBM Bluemix 7

Slide 8

Slide 8 text

8 Location Tracker Part 3: https://cloudant.com/location-tracker/

Slide 9

Slide 9 text

IBM Cloudant •  Globally distributed data layer for web and mobile applications •  MongoDB-style queries •  Advanced geospatial capabilities •  Full text search indexing 9

Slide 10

Slide 10 text

JSON Documents 10 {
 _id: "6EF9D2B0-13D3-1378-8D30-39E3CE0B36C2",
 _rev: "1-0b457efcf82fb29492ef927ba5b6ee15",
 type: "Feature",
 geometry: {
 type: "Point",
 coordinates: [
 -71.1028,
 42.3691
 ]
 },
 properties: {
 session_id: "3486b13f-7b8a-8a96-dfbf-9b82800e367f",
 timestamp: 1422928591717
 }
 }

Slide 11

Slide 11 text

PouchDB and Cloudant Replication 11

Slide 12

Slide 12 text

Managing User Access

Slide 13

Slide 13 text

PouchDB Authentication •  User authentication plugin for PouchDB and CouchDB https://github.com/nolanlawson/pouchdb-authentication •  Integrates with the CouchDB authentication API •  signup •  login •  session •  logout •  Does not map to the Cloudant security model 13

Slide 14

Slide 14 text

Mapping to the Cloudant Security Model 14 .! ├── LICENSE.txt! ├── Procfile! ├── README.md! ├── admin.js! ├── app.js! ├── controllers! ├── ddocs! ├── manifest.yml! ├── node_modules! ├── package.json! ├── public! ├── routes! │ └── api.js! ├── tutorial! └── views

Slide 15

Slide 15 text

Authentication Methods •  Registration (api.putUser) PUT /api/_users/:id" •  Login (api.postSession) POST /api/_session" •  Session (api.getSession) GET /api/_session" •  Logout and Cloudant Service Proxy •  API is the same in Cloudant for logout •  All other requests to /api/* are proxied to Cloudant 15

Slide 16

Slide 16 text

User Registration 1.  Generates a Cloudant API key and API password 2.  Grants the generated API key user read and write access to the location-tracker database 3.  AES encrypts the API key password using the user's chosen password as the encryption key 4.  Saves the user's metadata as a document to the users database 16

Slide 17

Slide 17 text

User Login 1.  Get's the user's document from the user database 2.  Decrypts the API password using the password supplied by the user 3.  Authenticates with Cloudant using the API key and decrypted API password 4.  Passes along the authentication cookie from Cloudant (if login was successful) 17

Slide 18

Slide 18 text

User Session 1.  Passing along the cookie from the client, calls the corresponding session function from the Cloudant Node.js client library 2.  Returns the response from Cloudant to the client 18

Slide 19

Slide 19 text

User Logout and Cloudant Service Proxy 19

Slide 20

Slide 20 text

20

Slide 21

Slide 21 text

Querying Locations By User

Slide 22

Slide 22 text

Locations by User •  Shows a list of users •  Click a user to see that user's map •  Cloudant Query https://cloudant.com/blog/introducing-cloudant-query/ •  PouchDB Find http://nolanlawson.github.io/pouchdb-find/ 22

Slide 23

Slide 23 text

Locations by User •  Shows a list of users •  Click a user to see that user's map •  Cloudant Query https://cloudant.com/blog/introducing-cloudant-query/ •  PouchDB Find http://nolanlawson.github.io/pouchdb-find/ 22

Slide 24

Slide 24 text

Querying a Database with PouchDB Find •  Based on Cloudant Query (Mango) •  Local or remote queries •  MongoDB-style query language •  Define fields to index 23 Image Credit: Mango with section on a white background by bangdoll, on Flickr

Slide 25

Slide 25 text

Defining an Index (admin.js) 24

Slide 26

Slide 26 text

25

Slide 27

Slide 27 text

Querying the Index (public/script/app.js) 26

Slide 28

Slide 28 text

Further Reading •  Location Tracker https://cloudant.com/location-tracker/ •  Cloudant Node.js library https://github.com/cloudant/nodejs-cloudant •  PouchDB http://pouchdb.com/ •  Cloudant For Developers https://cloudant.com/for-developers/ 27

Slide 29

Slide 29 text

Image Credits 28 •  Overview of a three-tier application, by Bartledan, based on a file by User:Foofy.Bartledan at en.wikipedia [Public domain], from Wikimedia Commons •  Mango with section on a white background by bangdoll, on Flickr

Slide 30

Slide 30 text

Bradley Holt Developer Advocate [email protected] @BradleyHolt github.com/bradley-holt