O'Reilly logo

Mastering Backbone.js by Abiee Echamea

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing HTTP Basic Authentication

Let's implement the Basic Auth protocol in Contacts App. As you have learned in the previous sections, you will need to add the Authorization header for every request that you make to the server in order to be authenticated. From the server side, you will need to read and parse this header.

A useful npm package to decode the Authorization header has been developed. With the basic-auth module, you can read the request headers and return an object with two fields: name and pass, these fields can be used to authenticate the user. For simplicity, we will use a hardcoded user and password, not a real database:

// server/basicAuthMiddleware.js var basicAuth = require('basic-auth'); var authorizationRequired = function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required