$30 off During Our Annual Pro Sale. View Details »

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

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

    View Slide

  2. 2

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  10. 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

    }

    }

    View Slide

  11. PouchDB and Cloudant Replication
    11

    View Slide

  12. Managing User Access

    View Slide

  13. 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

    View Slide

  14. 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

    View Slide

  15. 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

    View Slide

  16. 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

    View Slide

  17. 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

    View Slide

  18. 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

    View Slide

  19. User Logout and Cloudant Service Proxy
    19

    View Slide

  20. 20

    View Slide

  21. Querying Locations By User

    View Slide

  22. 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

    View Slide

  23. 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

    View Slide

  24. 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

    View Slide

  25. Defining an Index (admin.js)
    24

    View Slide

  26. 25

    View Slide

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

    View Slide

  28. 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

    View Slide

  29. 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

    View Slide

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

    View Slide