Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cloudant Webinar: Integrate User Management and...

Cloudant Webinar: Integrate User Management and Cloudant Query into a Node.js & Cloudant Application

Join us to explore how we transformed IBM Cloudant's Location Tracker application from a CouchApp into a full three-tier Node.js app. You’ll learn how to add user management functionality and leverage Cloudant Query to display a map of individual user locations within the app.

Bradley Holt

June 18, 2015
Tweet

More Decks by Bradley Holt

Other Decks in Programming

Transcript

  1. Integrate User Management and Cloudant Query into a Node.js &

    Cloudant Application IBM Cloudant Webinar Thursday, June 18, 2015 Bradley Holt, Developer Advocate @BradleyHolt
  2. 2

  3. 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
  4. PouchDB •  A database in your web browser •  Also

    works in Node.js •  Can synchronize with any database that implements the CouchDB Replication Protocol 4
  5. 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
  6. 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
  7. 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
  8. IBM Cloudant •  Globally distributed data layer for web and

    mobile applications •  MongoDB-style queries •  Advanced geospatial capabilities •  Full text search indexing 9
  9. 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
 }
 }
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 20

  17. 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
  18. 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
  19. 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
  20. 25

  21. 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
  22. 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 <https://commons.wikimedia.org/wiki/File:Overview_of_a_three-tier_application_vectorVersion.svg> •  Mango with section on a white background by bangdoll, on Flickr <https://www.flickr.com/photos/bangdoll/5665235102>